Clear text in EditText when entered

后端 未结 18 1625
醉话见心
醉话见心 2020-11-27 03:44

I\'m trying to set and onclicklistener so that when I click within the edittext element it will clear its current contents. Is there something wrong here? When I compile thi

18条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 04:26

    public EditText editField;
    public Button clear = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
        setContentView(R.layout.text_layout);
       this. editField = (EditText)findViewById(R.id.userName);
    this.clear = (Button) findViewById(R.id.clear_button);  
    this.editField.setOnClickListener(this);
    this.clear.setOnClickListener(this);
    @Override
    public void onClick(View v) {
    
        // TODO Auto-generated method stub
    if(v.getId()==R.id.clear_button){
    //setText will remove all text that is written by someone
        editField.setText("");
        }
    }
    

提交回复
热议问题