我在用户点击时更改了背景按钮。
但是我需要更改2个按钮的背景:当我点击btnSim时我需要更改btnSim背景和btnNao背景:
holder.btnNao.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
ImageButton botao = (ImageButton) arg0;
if (botao.getBackground().getConstantState()==getResources().getDrawable(R.drawable.rdb_off).getConstantState())
{
botao.setBackgroundResource(R.drawable.rdb_on);
}
else
botao.setBackgroundResource(R.drawable.rdb_off);
}
});
holder.btnSim.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
ImageButton botaoSim = (ImageButton) arg0;
if (botaoSim.getBackground().getConstantState()==getResources().getDrawable(R.drawable.rdb_off).getConstantState())
{
botaoSim.setBackgroundResource(R.drawable.rdb_on);
}
else
botaoSim.setBackgroundResource(R.drawable.rdb_off);
}
});
编辑我的更多代码:http://pastebin.com/Jf1ZDek5
答案 0 :(得分:0)
请注意if {}。
中的更改Button botao = (Button) findViewById(R.id.button_1);
Button botaoSim = (Button) findViewById(r.id.button_2);
holder.btnNao.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
ImageButton botao = (ImageButton) arg0;
if (botao.getBackground().getConstantState()==getResources().getDrawable(R.drawable.rdb_off).getConstantState())
{
botao.setBackgroundResource(R.drawable.rdb_on);
botaoSim.setBackgroundResource(R.drawable.rdb_on);
}
else
botao.setBackgroundResource(R.drawable.rdb_off);
}
});
holder.btnSim.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
ImageButton botaoSim = (ImageButton) arg0;
if (botaoSim.getBackground().getConstantState()==getResources().getDrawable(R.drawable.rdb_off).getConstantState())
{
botao.setBackgroundResource(R.drawable.rdb_on);
botaoSim.setBackgroundResource(R.drawable.rdb_on);
}
else
botaoSim.setBackgroundResource(R.drawable.rdb_off);
}
});
新代码段
public class MainActivity extends Activity implements OnClickListener {
@Override
public void onClick(View v) {
if (botaoSim.getBackground().getConstantState()==getResources().getDrawable(R.drawable.rdb_off).getConstantState())
{
botao.setBackgroundResource(R.drawable.rdb_on);
botaoSim.setBackgroundResource(R.drawable.rdb_on);
}
else
botaoSim.setBackgroundResource(R.drawable.rdb_off);
}
private class CustomAdapter extends BaseAdapter
{
/** All your implementations as in pastebin */
...
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
/* Add reference to holder.btnNao and holder.btnSim */
holder.btnNao.setOnClickListener(MainActivity.this);
holder.btnSim.setOnClickListener(MainActivity.this);
/* Your remaining code */
}
}
请注意,我已将customAdapter设为MainActivity中的私有内部类。
答案 1 :(得分:0)
您可以实现onClickListener
,因为两个按钮的一部分执行相同的操作:
示例:强>
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
botao.setOnClickListener(this);
botaoSim.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//put your code here.
}
}