在Android Edittext和Fragment中检查EditText是否为空

时间:2015-06-23 07:30:19

标签: android android-fragments android-edittext

我试图检查此edittext是否为空,然后显示Toast

这是我的片段:

public class First extends Fragment {
    Context mContext;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.intro1, container, false);
        return v;

        EditText txtun= (EditText) getView().findViewById(R.id.usredt);
        if ("".equals(txtun.getText().toString().trim())) {
            Toast.makeText(mContext, "You did not enter a value!", Toast.LENGTH_SHORT).show();
                  return;
        }
    }

}

这是我的 sliderLayout

public class SilderLayouts extends AppIntro2 {
    @Override
    public void init(Bundle savedInstanceState) {
        addSlide(new First(), getApplicationContext());
        addSlide(new Second(), getApplicationContext());
        addSlide(new Third(), getApplicationContext());
    }

    private void loadMainActivity(){
        Intent intent = new Intent(this, com.client.test.StartActivity.class);
        startActivity(intent);
    }

    @Override
    public void onDonePressed() {
        loadMainActivity();
    }


}

我尝试了类似的事情:

Context@Override上方添加 First.this,

中的Toast

但它没有用。

请记住,Return中存在问题,如果此回复已删除,则Toast中存在问题。

如何实现这一目标?

感谢。

3 个答案:

答案 0 :(得分:0)

试试这个

 EditText txtun= (EditText) v.findViewById(R.id.usredt);
    if (txtun.getText().toString().compareToIgnoreCase("") == 0) {
        Toast.makeText(mContext, "You did not enter a value!", Toast.LENGTH_SHORT).show();
              return;
    }

答案 1 :(得分:0)

试试这个来解决你的问题

class First extends FragmentActivity {
        Context mContext;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View v = View.inflate(getApplicationContext(), R.layout.intro1, null);


            EditText txtun= (EditText) v.findViewById(R.id.usredt);
            if (TextUtils.isEmpty(txtun.getText().toString().trim())) {
                Toast.makeText(getApplicationContext(), "You did not enter a value!", Toast.LENGTH_SHORT).show();
            }
            return v;
        }

    }

答案 2 :(得分:0)

我认为问题在于使用上下文。尝试这样的事情:

public class First extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.intro1, container, false);
        EditText txtun= (EditText) getView().findViewById(R.id.usredt);
        if ("".equals(txtun.getText().toString().trim())) {
            Toast.makeText(getActivity(), "You did not enter a value!", Toast.LENGTH_SHORT).show();
        }

        return v;
    }

}