问题
I have an editText and have added an onClickListener to it. In the click method I am just clearing the text. When I click the editText first time the keypad pops up. But it is not going into the onClick method. The second time when I click it it is called and clears the text.
qtyEditTxt=(EditText)findViewById(R.id.qtyet);
qtyEditTxt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
qtyEditTxt.setText("");
}
});
I saw a question with an answer stating to make android:focusableInTouchMode to false. But then I will not be able to enter the text. Kindly help me with this
回答1:
How about focus?
final EditText qtyEditTxt= (EditText) findViewById(R.id.qtyet);
qtyEditTxt.setOnFocusChangeListener(new OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean isFocus)
{
if (isFocus)
{
qtyEditTxt.setText("");
}
}
});
Edited:
Default text? There you go :)
android:hint="Enter Quantity"
回答2:
The behavior you are trying to have is very similar with an already existing feature of EditText.
Take a look at: http://developer.android.com/reference/android/widget/TextView.html#attr_android:hint
回答3:
ClickListener is not a good choice for editText . use
editText.setOnEditorActionListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
})
}
回答4:
Just add <requestFocus /> right after closing tag of EditText in your layout.xml file
来源:https://stackoverflow.com/questions/8397609/onclicklistener-listens-only-on-the-second-time