在Android中为侦听器添加一个switch case

时间:2013-12-16 03:37:06

标签: java android switch-statement listener

我有一个方法onFocusChange(),此方法用于检查我的应用上的edittext是否为焦点,如下所示:

nama_pp = (EditText) polis.findViewById(R.id.nama_pp);
nama_pp.setOnFocusChangeListener(this);
gelar_pp = (EditText) polis.findViewById(R.id.gelar_pp);
gelar_pp.setOnFocusChangeListener(this);
alamat_pp = (EditText) polis.findViewById(R.id.alamat_pp);
alamat_pp.setOnFocusChangeListener(this);
kota_pp = (EditText) polis.findViewById(R.id.kota_pp);
kota_pp.setOnFocusChangeListener(this);
kdpos_pp = (EditText) polis.findViewById(R.id.kdpos_pp);
kdpos_pp.setOnFocusChangeListener(this);

以下是方法:

public void onFocusChange(View v, boolean hasFocus) {
    switch (v.getId()) {
        layout_alamatrmh_pp.setBackgroundResource(R.drawable.border_corner_baris1);
    }else{
        layout_alamatrmh_pp.setBackgroundResource(R.drawable.border_corner_baris1_klik);
    }
    case R.id.alamat_pp:
    if (!hasFocus) {
        layout_alamatrmh_pp.setBackgroundResource(R.drawable.border_corner_baris1);
    } else {
        layout_alamatrmh_pp.setBackgroundResource(R.drawable.border_corner_baris1_klik);
    }
    break;
    case R.id.kota_pp:
    if (!hasFocus) {
        layout_alamatrmh_pp.setBackgroundResource(R.drawable.border_corner_baris1);
    } else {
        layout_alamatrmh_pp.setBackgroundResource(R.drawable.border_corner_baris1_klik);
    }
    break;
    case R.id.kdpos_pp:
    if (!hasFocus) {
        layout_alamatrmh_pp.setBackgroundResource(R.drawable.border_corner_baris1);
    } else {
        layout_alamatrmh_pp.setBackgroundResource(R.drawable.border_corner_baris1_klik);
    }
    break;
    case R.id.telp1_pp:
    if (!hasFocus) {
        layout_alamatrmh_pp.setBackgroundResource(R.drawable.border_corner_baris1);
    } else {
        layout_alamatrmh_pp.setBackgroundResource(R.drawable.border_corner_baris1_klik);
    }
    break;
    case R.id.telp2_pp:
    if (!hasFocus) {
        layout_alamatrmh_pp.setBackgroundResource(R.drawable.border_corner_baris1);
    } else {
        layout_alamatrmh_pp.setBackgroundResource(R.drawable.border_corner_baris1_klik);
    }
    break;
}

如您所见,alamat_pp,kota_pp,kd_pos_pp的语句具有相同的陈述:

layout_alamatrmh_pp.setBackgroundResource(R.drawable.border_corner_baris1);
        }else {
layout_alamatrmh_pp.setBackgroundResource(R.drawable.border_corner_baris1_klik);
        }

如果我想将我的开关设置为监听器的焦点更改为alamat_pp.setOnFocusChangeListener(listener);,我将如何在switch中编写它?

public void onFocusChange(View v, boolean hasFocus) {
    switch(v.getId()){
        layout_alamatrmh_pp.setBackgroundResource(R.drawable.border_corner_baris1);
    }else {
        layout_alamatrmh_pp.setBackgroundResource(R.drawable.border_corner_baris1_klik);
    }
    case R.id.alamat_pp:
    if(!hasFocus){
        layout_alamatrmh_pp.setBackgroundResource(R.drawable.border_corner_baris1);
    }else {
        layout_alamatrmh_pp.setBackgroundResource(R.drawable.border_corner_baris1_klik);
    }
    break;
}
switch(?????){
    case listener:
        if(!hasFocus){
            layout_alamatrmh_pp.setBackgroundResource(R.drawable.border_corner_baris1);
        }else {
            layout_alamatrmh_pp.setBackgroundResource(R.drawable.border_corner_baris1_klik);
        }
        break;
}
}

1 个答案:

答案 0 :(得分:0)

 //implement the onFocusChange method here
        public void onFocusChange(View v) {
           // Perform action on FocusChange
          switch(v.getId()) {
            case R.id.buttonplay:
              //Play voicefile
              MediaPlayer.create(getBaseContext(), R.raw.voicefile).start();
              break;
            case R.id.buttonstop:
              //Stop MediaPlayer
              MediaPlayer.create(getBaseContext(), R.raw.voicefile).stop();
              break;
          }
       }

在情况条件下传递按钮ID ....