我正在使用此代码检查我的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);
                }
            }
        };

我的代码无法正常工作,没有任何变化。我的代码有什么问题吗?

最佳答案

首先初始化监听器,然后将其设置为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);

如果以前没有设置过,则将EditTexts的focusable属性设置为true
editText.setFocusable(true);

08-06 12:00