简体   繁体   中英

Android Editext: Layout with 2 Edit Text fields needs 2 click to clear the text?

I have 2 edit text fiels on the layout name MainActivity.

<EditText
    android:id="@+id/edt1"/>

<EditText
    android:id="@+id/edt2"/>

I want to clear the text when I click on the text field. In case I have 1 text field , when I click on it, the text will be clear immediately.

However, in my situation (2 text fields), when I enter text for field 1, then field 2.

Then click to field 1 again, and it need 2 clicks to clear the text.

I guess the problem related to Focusable

Java code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    edt1 = (EditText) findViewById(R.id.edt1);
    edt1.setOnClickListener(this);

    edt2 = (EditText) findViewById(R.id.edt2);
    edt2.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    switch (v.getId()) {
        case R.id.edt1:
            edt1.setText("");
            break;
        case R.id.edt2:
            edt2.setText("");
            break;

    }

}

Any one can help my? I really appreciate your help. Thank you very much in advanced.

Use focusChnagedListener on EdiText

edit_Text.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
    if(hasFocus){
       // clear the text
    }
   }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM