Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Edittext : setOnFocusChangeListener doesn't work

i'm using this code for checking my edittext focus or not :

 gelar_pp=(EditText)polis.findViewById(R.id.gelar_pp);
    gelar_pp.setOnFocusChangeListener(new OnFocusChangeListener() {
    LinearLayout layout_nama_pp=(LinearLayout)findViewById(R.id.layout_nama_pp);
        public void onFocusChange(View v, boolean hasFocus) {
            if(!hasFocus){
                layout_nama_pp.setBackgroundResource(R.drawable.border_corner_baris1);
            }else {
                layout_nama_pp.setBackgroundResource(R.drawable.border_corner_baris1_klik);
            }
        } });
    ibu_pp=(EditText)polis.findViewById(R.id.ibu_pp);
    ibu_pp.setOnFocusChangeListener(new OnFocusChangeListener() {
    LinearLayout layout_nama_pp=(LinearLayout)findViewById(R.id.layout_nama_pp);
        public void onFocusChange(View v, boolean hasFocus) {
            if(!hasFocus){
                layout_nama_pp.setBackgroundResource(R.drawable.border_corner_baris1);
            }else {
                layout_nama_pp.setBackgroundResource(R.drawable.border_corner_baris1_klik);
            }
        } });
    edit_bukti_lain_pp=(EditText)polis.findViewById(R.id.edit_bukti_lain_pp);
    edit_bukti_lain_pp.setOnFocusChangeListener(new OnFocusChangeListener() {
    LinearLayout layout_nama_pp=(LinearLayout)findViewById(R.id.layout_nama_pp);
        public void onFocusChange(View v, boolean hasFocus) {
            if(!hasFocus){
                layout_nama_pp.setBackgroundResource(R.drawable.border_corner_baris1);
            }else {
                layout_nama_pp.setBackgroundResource(R.drawable.border_corner_baris1_klik);
            }
        } });

when i make simple my code, and change into :

    gelar_pp.setOnFocusChangeListener(listener);
ibu_pp.setOnFocusChangeListener(listener);
edit_bukti_lain_pp.setOnFocusChangeListener(listener);
        listener= new OnFocusChangeListener() {    
            LinearLayout layout_nama_pp=(LinearLayout)findViewById(R.id.layout_nama_pp);
            public void onFocusChange(View v, boolean hasFocus) {
                if(!hasFocus){
                    layout_nama_pp.setBackgroundResource(R.drawable.border_corner_baris1);
                }else {
                    layout_nama_pp.setBackgroundResource(R.drawable.border_corner_baris1_klik);
                }
            }
        };

my code is not working, nothing change. is there any wrong with my code?

like image 710
Aoyama Nanami Avatar asked Nov 26 '13 11:11

Aoyama Nanami


2 Answers

Try like this

edit_Text.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
    if(hasFocus){
        Toast.makeText(getApplicationContext(), "on focus", Toast.LENGTH_LONG).show();
    }else {
        Toast.makeText(getApplicationContext(), "lost focus", Toast.LENGTH_LONG).show();
    }
   }
});
like image 103
Tombeau Avatar answered Oct 30 '22 01:10

Tombeau


Add in XML

android:focusable="true"
android:focusableInTouchMode="true"
like image 30
Dayanand Waghmare Avatar answered Oct 30 '22 01:10

Dayanand Waghmare