Android:按下了哪个按钮

时间:2013-07-07 12:31:02

标签: android button

我找到了以下解决方案来检测按下了哪些按钮:

public void onClick(View v) {
    switch(v.getId())
    {
    case R.id.bteins:
    Toast t = Toast.makeText(this, "bzeins", Toast.LENGTH_SHORT);
    t.show();
    break;
    case R.id.btzwei:
    // handle button B click;
    break;
    case R.id.btdrei:
        Toast t1 = Toast.makeText(this, "btdfrei", Toast.LENGTH_SHORT);
        t1.show();
    break;
    case R.id.btvier:
    // handle button B click;
    break;
    default:
    throw new RuntimeException("Unknow button ID");
    }
    }

我的问题是,当我按bteinsbtdrei时,没有任何反应。我的错误是什么?

4 个答案:

答案 0 :(得分:1)

您可以为每个按钮实施onClick,如下所示:

    findViewById(R.id.bteins).setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            //do some things!
        }
    });
    findViewById(R.id.btzwei).setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            //do some things!
        }
    });
....

或在活动中实施OnClickListener并将所有按钮设置为单击:

public class Test extends Activity implements OnClickListener
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        findViewById(R.id.bteins).setOnClickListener(this);
        findViewById(R.id.btzwei).setOnClickListener(this);
        findViewById(R.id.btdrei).setOnClickListener(this);
        findViewById(R.id.btvier).setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        switch(v.getId())
            {
            case R.id.bteins:
                Toast t = Toast.makeText(this, "bzeins", Toast.LENGTH_SHORT);
                t.show();
                break;
            case R.id.btzwei:
                // handle button B click;
                break;
            case R.id.btdrei:
                    Toast t1 = Toast.makeText(this, "btdfrei", Toast.LENGTH_SHORT);
                    t1.show();
                break;
            case R.id.btvier:
                // handle button B click;
                break;
            default:
                throw new RuntimeException("Unknow button ID");
            }
    }
}

答案 1 :(得分:0)

您是否已将OnClickListener设为按钮?我想你没有。

答案 2 :(得分:0)

public class theexample extends Activity implements OnClickListener

//OnCreateEvent
bteins.setOnClickListener(this);
btvier.setOnClickListener(this);

public void OnClick(View v){
switch(v.getId()){
            case R.id.bteins:
                Toast.makeText(this, "bzeins", Toast.LENGTH_SHORT).show();
                t.show();
                break;
            case R.id.btzwei:
                // handle button B click;
                break;
            case R.id.btdrei:
                   Toast.makeText(this, "btdfrei", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btvier:
                // handle button B click;
                break;
            default:
                throw new RuntimeException("Unknow button ID");
            }
    }

答案 3 :(得分:0)

您需要使用setOnClickListener()

将触发器连接到各自的处理程序

此方法要么this作为参数,要么你的活动必须实现OnClickListener(Eclipse可以帮助你轻松地自动重构这些东西。)

或者,只需在现场传递一个新的OnClickListener:

Button myButton = (Button) findViewById(*buttons id*);
myButton.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        //You can check v attributes to see what was clicked as well. 
    }
});