2

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.

Yugesh
  • 4,030
  • 9
  • 57
  • 97
DilAka
  • 51
  • 1
  • 8
  • I think first time I saw that someone have implemented onClick Listener for EditText. By the way What you are trying to do? You should use http://developer.android.com/reference/android/view/View.OnFocusChangeListener.html – Pankaj Kumar Feb 07 '14 at 05:48

7 Answers7

1

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("");

        }
    }
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
  • do you want to implement kind of hint functionality? – Biraj Zalavadia Feb 07 '14 at 06:12
  • Actually this is for Custom BaseAdpter. Click event in my main Activity , in my App it contain lot of clickevent in custom dialog so i defined all these in single method in my mainActivity. I am going through this problem from last two days , at last i post this thread. Please help me to get me out of this issue. – DilAka Feb 07 '14 at 06:20
  • Without android:focusable="......" , program execution is not entering in if{ } block. – DilAka Feb 07 '14 at 06:23
1

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();
    }
   }
});
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • Thanks for suggestion , but as i told i can't handle it in your way , i am using custom BaseAdater and i declare it as setter-getter . Click event in mainActivity . If i used as per your suggestion i am getting NullPointer Exception , because edt_xtx is not declare in my mainActivity. – DilAka Feb 07 '14 at 06:50
1

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.

Ajay J G
  • 886
  • 7
  • 21
0

you need to set the text

change edtxtx.getText("");to

edtxtx.setText("");
Nambi
  • 11,944
  • 3
  • 37
  • 49
0

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.

saa
  • 1,538
  • 2
  • 17
  • 35
0

Instead of setting android:focusable="false" in xml ... write this in your code on onCreate() :

    if (EditText.hasFocus()) {
        EditText.clearFocus();
   }
Amresh
  • 2,098
  • 16
  • 20
0

Just add this attrs in your Edittext:

android:focusableInTouchMode="false"
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
LVS
  • 76
  • 1
  • 3