0

通过在 android 中使用多个按钮来执行单个操作,例如按下 button1 和 button2,我们会得到一个操作。解决这个问题的最佳方法是什么

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.braill);
    Button btn1 = (Button) findViewById(R.id.btn1);
    Button btn2 = (Button) findViewById(R.id.btn2);
    Button btn3 = (Button) findViewById(R.id.btn3);
    Button btn4 = (Button) findViewById(R.id.btn4);
    Button btn5 = (Button) findViewById(R.id.btn5);
    Button btn6 = (Button) findViewById(R.id.btn6);
    Button complete = (Button) findViewById(R.id.complete);
    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);
    btn3.setOnClickListener(this);
    btn4.setOnClickListener(this);
    btn5.setOnClickListener(this);
    btn6.setOnClickListener(this);
    complete.setOnClickListener(this);



}

@Override
public void onClick(View v) {
    final EditText edittext = (EditText) findViewById(R.id.edtext);
    switch(v.getId()){
    case R.id.btn1 & R.id.btn2 & R.id.complete:
    {
        Toast toast = Toast.makeText(getApplicationContext(), "Button 1", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER,0, 0);
        toast.show();
        edittext.append("a");
        break;  
    }


}

}}

4

4 回答 4

1

把你的开关变成一个有效的 if 条件

if ( (v.getId()==R.id.btn1) || (v.getId()==R.id.btn2) || (v.getId()==R.id.complete))
{
    Toast toast = Toast.makeText(getApplicationContext(), "Button 1", Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER,0, 0);
    toast.show();
    edittext.append("a");
    break;  
}
于 2013-11-12T08:05:31.310 回答
1

将所有按钮放在 case 语句中,不要与它们一起添加break,在最后一个 case 语句中只需添加您的逻辑并放置break语句。

    @Override
    public void onClick(View v) {
        final EditText edittext = (EditText) findViewById(R.id.edtext);
        switch(v.getId()){
        case R.id.btn1: 
                 //no break; continue executing next.
        case R.id.btn2: 
              //no break; continue executing next.
        case R.id.complete:
            Toast toast = Toast.makeText(getApplicationContext(), "Button 1", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER,0, 0);
            toast.show();
            edittext.append("a");
//break here.
            break;  
    ...
        }

}

于 2013-11-12T08:07:16.833 回答
0

尝试这个

button3333.setOnClickListener(getOnClickDoSomething(button3333)); button2222.setOnClickListener(getOnClickDoSomething(button2222));

getOnClickDoSomething( )的实现

View.OnClickListener getOnClickDoSomething(final Button button)  {
return new View.OnClickListener() {
    public void onClick(View v) {
        button.setText("text now set.. ");    
    }
};

}

于 2013-11-12T08:22:09.233 回答
0

我将介绍一个 OnClickListener 的匿名实例,您可以将其设置为相关按钮的侦听器。这很好地封装了行为,您不必交叉引用按钮 ID:

private OnClickListener mDoWhatever = new OnClickListener() {
    @Override
    public void onClick(View v) {
        final EditText edittext = (EditText) findViewById(R.id.edtext);
        Toast toast = Toast.makeText(getApplicationContext(), "Button 1", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER,0, 0);
        toast.show();
        edittext.append("a");
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.braill);
    Button btn1 = (Button) findViewById(R.id.btn1);
    Button btn2 = (Button) findViewById(R.id.btn2);
    Button btn3 = (Button) findViewById(R.id.btn3);
    Button btn4 = (Button) findViewById(R.id.btn4);
    Button btn5 = (Button) findViewById(R.id.btn5);
    Button btn6 = (Button) findViewById(R.id.btn6);
    Button complete = (Button) findViewById(R.id.complete);
    btn1.setOnClickListener(mDoWhatever);
    btn2.setOnClickListener(mDoWhatever);
    btn3.setOnClickListener(this);
    btn4.setOnClickListener(this);
    btn5.setOnClickListener(this);
    btn6.setOnClickListener(this);
    complete.setOnClickListener(mDoWhatever);
  }
于 2013-11-12T08:23:23.660 回答