无法在android中使用edittext.setText(“”)清除edittext

时间:2014-05-05 13:04:46

标签: android button android-edittext reset

我正在尝试清除或说明在单击“清除表单”按钮时重置editText。我无法清除editText框。这是我的完整代码:

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_us);

    et1 = (EditText) findViewById(R.id.editText1); //name
    et3 = (EditText) findViewById(R.id.editText3); //email
    et2 = (EditText) findViewById(R.id.editText2); //feedback


    Button submit1 = (Button) findViewById(R.id.button1);
    //submit1.setOnClickListener(this);
    Button clear=(Button)findViewById(R.id.button2);
    //clear.setOnClickListener(this);

    submit1.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
             if(view==submit1)
             {
                if(et1.getText().toString().length()==0 || et2.getText().toString().length()==0
                        || et3.getText().toString().length()==0 )
                {
                    Toast t1=Toast.makeText(getApplicationContext(), "Please fill all the forms",
                            Toast.LENGTH_LONG);
                    t1.show();
                }
                else
                {
                    String email = "user@gmail.com"; 

                    String sub1=spin.getSelectedItem().toString();
                    String message = et2.getText().toString();//body
                    String name = et1.getText().toString(); 

                    String from=et3.getText().toString();
                    String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";

                    if(from.matches(emailPattern))
                    {
                        String body="Name :"+name+"\n"+"Email ID :"+ from+"\n"+"Feedback :"+message;
                        sendMail(email,sub1,from, body); 
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "Invalid Email ID", Toast.LENGTH_SHORT).show();
                        et3.setText("");
                    }
                }
            }
             else if(view==clear)
                {
                    et1.setText("");
                    et2.setText("");
                    et3.setText("");
                }
        }
    });
}

任何改变.. ??我哪里错了.. ??请帮帮我......

3 个答案:

答案 0 :(得分:2)

您忘了为onClickListener添加清除按钮,更改下面的代码,

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_us);

    et1 = (EditText) findViewById(R.id.editText1); //name
    et3 = (EditText) findViewById(R.id.editText3); //email
    et2 = (EditText) findViewById(R.id.editText2); //feedback


    Button submit1 = (Button) findViewById(R.id.button1);
    //submit1.setOnClickListener(this);
    Button clear=(Button)findViewById(R.id.button2);
    //clear.setOnClickListener(this);

    submit1.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
             if(view==submit1)
             {
                if(et1.getText().toString().length()==0 || et2.getText().toString().length()==0
                        || et3.getText().toString().length()==0 )
                {
                    Toast t1=Toast.makeText(getApplicationContext(), "Please fill all the forms",
                            Toast.LENGTH_LONG);
                    t1.show();
                }
                else
                {
                    String email = "user@gmail.com"; 

                    String sub1=spin.getSelectedItem().toString();
                    String message = et2.getText().toString();//body
                    String name = et1.getText().toString(); 

                    String from=et3.getText().toString();
                    String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";

                    if(from.matches(emailPattern))
                    {
                        String body="Name :"+name+"\n"+"Email ID :"+ from+"\n"+"Feedback :"+message;
                        sendMail(email,sub1,from, body); 
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "Invalid Email ID", Toast.LENGTH_SHORT).show();
                        et3.setText("");
                    }
                }
            }
        }
    });

    // Add this code
    clear.setOnClickListener(new View.OnClickListener()
    {
            @Override
            public void onClick(View view)
            {
                if(view==clear)
                {
                    et1.setText("");
                    et2.setText("");
                    et3.setText("");
                }
            }
    });
}

答案 1 :(得分:0)

在点击侦听器上实施清除按钮: -

clear.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {

                    et1.setText("");
                    et2.setText("");
                    et3.setText("");

        }
    });

答案 2 :(得分:0)

试试这个,

EditText et1;
et1.setText(null);