为什么setOnclick Button无法正常工作?

时间:2012-06-23 07:52:00

标签: android

我使用android 2.3.3 我的代码:

    read=(Button)findViewById(R.id.bt_read);
    write=(Button)findViewById(R.id.bt_write);
    text=(EditText)findViewById(R.id.editText1);

    // write file
    write.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Write", Toast.LENGTH_SHORT);
        }
    });

但是当我点击按钮Toast not show。谁能告诉我为什么?感谢

3 个答案:

答案 0 :(得分:1)

更改

Toast.makeText(getApplicationContext(), "Write", Toast.LENGTH_SHORT);

Toast.makeText(getApplicationContext(), "Write", Toast.LENGTH_SHORT).show();

答案 1 :(得分:1)

您必须添加show方法才能显示Toast View

Toast.makeText(getApplicationContext(), "Write", Toast.LENGTH_SHORT).show();

没有显示Toast,您的代码如下所示

Toast.makeText(getApplicationContext(), "Write", Toast.LENGTH_SHORT);

toast view有关详细信息,请参阅此文档http://developer.android.com/guide/topics/ui/notifiers/toasts.html

答案 2 :(得分:0)

在Toast结束时,您需要添加“.show()”。

代码:

 Toast.makeText(getApplicationContext(), "Write", Toast.LENGTH_SHORT).show();