Reputation: 51
I am going through a problem .
XML Coding
<EditText
android:id="@+id/edt_txt_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false"
android:onClick="editTextClick" >
</EditText>
Java Coding
public void editTextClick(View v) {
if (v.getId() == R.id.edt_txt_id){
System.out.println(" edit text click");
EditText edtxtx =(EditText)v;
edtxtx.requestFocus();
edtxtx.setText("");
}
}
I want that when i click on editText then current text must be dissapperar. But when i click with
android:focusable="false"
Click event work fine but cursor is no longer at Edit-text. It means if want to enter some new Text then how could enter this new text even cursor is not at Edit-text , and if i work with
android:focusable="true"
then click event does not work. But is available for any new edit .What is problem ? I know it is silly mistake , but where , i can't figure out.Thanks in advance to all.
Upvotes: 2
Views: 5105
Reputation: 926
I know the question was asked long back, thought my answer would help someone. I faced the same issue that editText was not focusing on click.
I had the below code to my EditText's parent layout which was blocking the keyboard.
android:descendantFocusability="blocksDescendants"
Remove this and try it worked for me.
Upvotes: 1
Reputation: 86
Just add this attrs in your Edittext
:
android:focusableInTouchMode="false"
Upvotes: 0
Reputation: 23638
You need to implement the setonFocusChange
listener for your EditText
to achieve the functionality which you want.
Here is the focus listener example.
edtxtx.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
Toast.makeText(getApplicationContext(), "got the focus", Toast.LENGTH_LONG).show();
edtxtx.setText("");
}else {
Toast.makeText(getApplicationContext(), "lost the focus", Toast.LENGTH_LONG).show();
}
}
});
Upvotes: 1
Reputation: 2108
Instead of setting android:focusable="false"
in xml ... write this in your code on onCreate()
:
if (EditText.hasFocus()) {
EditText.clearFocus();
}
Upvotes: 0
Reputation: 1568
Try this
public void editTextClick(View v) {
if (v.getId() == R.id.edt_txt_id){
System.out.println(" edit text click");
EditText edtxtx =(EditText)v;
edtxtx.setFocusable(true);
edtxtx.setText("");
}}
After this again setFocusable(false);
So that it can take click event again.
Upvotes: 0
Reputation: 28484
Try this way
public void editTextClick(View v) {
if (v.getId() == R.id.edt_txt_id) {
System.out.println(" edit text click");
EditText edtxtx = (EditText) v;
edtxtx.setFocusable(true); // Add This Line And try
edtxtx.requestFocus();
edtxtx.setText("");
}
}
Upvotes: 1
Reputation: 12042
you need to set the text
change edtxtx.getText("");
to
edtxtx.setText("");
Upvotes: 0