我的应用程序包含EditText
的某些实例。我必须清理所有EditText
单击按钮。我需要写一个条件来清理所有EditText
其中EditText
个为空。我将如何做?
public class Fragment1 extends Fragment implements View.OnClickListener
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v=inflater.inflate(R.layout.fragment1,null);
seekBar1 = v.findViewById(R.id.fragment1SeekBar1);
seekBar2 = v.findViewById(R.id.fragment1SeekBar2);
seekBar3=v.findViewById(R.id.fragment1SeekBar3);
edTxt1=v.findViewById(R.id.fragment1EditText1);
edTxt2=v.findViewById(R.id.fragment1EditText2);
edTxt3=v.findViewById(R.id.fragment1EditText3);
fragment1button1=v.findViewById(R.id.fragment1Button1);
fragment1button1.setOnClickListener(this);}
@Override
public void onClick(View view){
int val1 = Integer.parseInt( edTxt1.getText().toString());
int val2=Integer.parseInt( edTxt2.getText().toString());
int val3=Integer.parseInt( edTxt3.getText().toString());
switch(view.getId()){
case R.id.fragment1Button1:
if(edTxt1.equals("")){
edTxt1.getText().clear();
edTxt2.getText().clear();
edTxt3.getText().clear();
}else if(edTxt2.equals("")){
edTxt1.getText().clear();
edTxt2.getText().clear();
edTxt3.getText().clear();}
else if (edTxt3.equals("")){
edTxt1.getText().clear();
edTxt2.getText().clear();
edTxt3.getText().clear();}
break;}
}
}
答案 0 :(得分:1)
if(edTxt1.equals(""))
您的条件不正确。他们应该看起来像这样:
if(edTxt1.getText().toString().equals(""))
答案 1 :(得分:1)
如果要将编辑文本的文本设置为null:
edTxt1.setText("");
否则,您可以将其设置为不可见而消失。
edTxt1.setVisibility(View.INVISIBLE);
答案 2 :(得分:1)
如果要将“ EditText”设置为空,请使用
if(edTxt1.getText().toString().equals("")) {
edTxt1.setText("");
edTxt2.setText("");
edTxt3.setText("");
} else if(edTxt2.getText().toString().equals("")) {
edTxt1.setText("");
edTxt2.setText("");
edTxt3.setText("");
} else if (edTxt3.getText().toString().equals("")) {
edTxt1.setText("");
edTxt2.setText("");
edTxt3.setText("");
}
break;
或者如果您想删除edittext
if (edTxt1.getText().toString().equals("")) {
edTxt1.setVisibility(View.GONE);
edTxt2.setVisibility(View.GONE);
edTxt3.setVisibility(View.GONE);
} else if(edTxt2.getText().toString().equals("")) {
edTxt1.setVisibility(View.GONE);
edTxt2.setVisibility(View.GONE);
edTxt3.setVisibility(View.GONE);
} else if (edTxt3.getText().toString().equals("")) {
edTxt1.setVisibility(View.GONE);
edTxt2.setVisibility(View.GONE);
edTxt3.setVisibility(View.GONE);
}
break;
答案 3 :(得分:1)
尝试这种方式
switch(view.getId())
{
case R.id.fragment1Button1:
if (TextUtils.isEmpty(edTxt1.getText().toString())) {
edTxt1.getText().clear();
edTxt2.getText().clear();
edTxt3.getText().clear();
} else if (TextUtils.isEmpty(edTxt2.getText().toString())) {
edTxt1.getText().clear();
edTxt2.getText().clear();
edTxt3.getText().clear();
} else if (TextUtils.isEmpty(edTxt3.getText().toString())) {
edTxt1.getText().clear();
edTxt2.getText().clear();
edTxt3.getText().clear();
}
break;
}
答案 4 :(得分:0)
每EditText
都这样做
if (!edTx.getText().toString().equals("")) {
edTx.setText("");
}
答案 5 :(得分:0)
尝试一下 如果条件改变
if(edTxt1.equals(""))
到
if(edTxt1.getText().toString().length()>0)
让我知道是否可行