简体   繁体   中英

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?

Add in XML

android:focusable="true"
android:focusableInTouchMode="true"

First initialize the listener and then set to EditTexts like

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);
            }
        }
    };
gelar_pp.setOnFocusChangeListener(listener);
ibu_pp.setOnFocusChangeListener(listener);
edit_bukti_lain_pp.setOnFocusChangeListener(listener);

and set focusable property to true for EditTexts if not set previously like...

editText.setFocusable(true);

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

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