Android Edittext:setOnFocusChangeListener不起作用

时间:2013-11-26 11:11:20

标签: java android android-edittext

我正在使用此代码检查我的edittext焦点:

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

当我简化我的代码时,改为:

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

我的代码无效,没有任何变化。我的代码有什么问题吗?

3 个答案:

答案 0 :(得分:5)

添加XML

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

答案 1 :(得分:3)

首先初始化侦听器,然后设置为

之类的EditTexts
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);

如果之前未设置为...

,则将可聚焦属性设置为true以用于EditTexts
editText.setFocusable(true);

答案 2 :(得分:1)

试试这个

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