当EditText不为空时显示TextView

时间:2018-11-19 09:24:02

标签: android android-studio

我正在使用Android应用程序,并且遇到以下问题。

问题::当两个edittext字段不为空时(当在edittext字段中写入文本时),我希望TextView显示一些文本。

我所做的事情:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle   
savedInstanceState) {
View rootView = inflater.inflate(R.layout.text_to_speech, container, false);  
...

if(edittextbox1.getText().toString().length() !=0 && edittextbox2.getText().toString().length() !=0) {
    textview.setText("some text")
} 
return rootView;
}

这不起作用。文本视图永远不会弹出。有什么想法吗?

4 个答案:

答案 0 :(得分:5)

尝试将TextWatcher添加到这两个文本中。您可以更改两个编辑文本的文本,然后获取长度,并在条件为真时显示textView。

TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int st, int b, int c)
        {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int st, int c, int a)
        {

        }

        @Override
        public void afterTextChanged(Editable s)
        {
            if(edittextbox1.getText().toString().length() != 0 && edittextbox2.getText().toString().length() != 0) {
                 textView.setVisibility(View.VISIBLE);
                 textview.setText("some text");
             }
        }
    };

  edittextbox1.addTextChangedListener(textWatcher);
  edittextbox2.addTextChangedListener(textWatcher);

答案 1 :(得分:3)

您需要使用类似的方法来收听您的EditText字段。

edittextbox1 = (EditText)findViewById(R.id.editText);

edittextbox1.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int st, int b, int c) 
    {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int st, int c, int a) 
    {
       if(edittextbox1.getText().toString().length() !=0) {
       textview.setText("some text")
  } 
    }

    @Override
    public void afterTextChanged(Editable s) 
    {
        if(edittextbox1.getText().toString().length() !=0) {
        textview.setText("some text")
  } 
    }
});

答案 2 :(得分:0)

请尝试设置textview的可见性

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle   
savedInstanceState) {
View rootView = inflater.inflate(R.layout.text_to_speech, container, false);  
...

if(edittextbox1.getText().toString().length() !=0 && edittextbox2.getText().toString().length() !=0) {
    textview.setText("some text");
     textview.setVisibility(View.VISIBLE);
} 
return rootView;
}

答案 3 :(得分:-1)

这样做。它将起作用!

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle   
savedInstanceState) {
   View rootView = inflater.inflate(R.layout.text_to_speech, container, false); 
   EditText edittextbox1=(EditText)rootView.findViewById(R.id.your_edittext_id1); 
   EditText edittextbox2=(EditText)rootView.findViewById(R.id.your_edittext_id2);
   TextView textView=(TextView)rootView.findViewById(R.id.your_textview_id1);

   if ((edittextbox1.getText().toString().length() != 0) && (edittextbox2.getText().toString().length() != 0)) {
        textView.setVisibility(View.VISIBLE);
        textview.setText("some text");
   } else {
        textView.setVisibility(View.GONE);
   }
   return rootView;
}