简体   繁体   English

按钮单击Android上的事件

[英]Button Click Event on Android

This is going to be a real noob question, so please have mercy. 这将是一个真正的noob问题,所以请怜悯。 I am trying to create a message box on a button click event in Android. 我正在尝试在Android中的按钮单击事件上创建一个消息框。 I have read some examples on StackOverflow, but I can't seems to grasp the concept. 我已经阅读了StackOverflow的一些例子,但我似乎无法掌握这个概念。 In my main.xml file, I have defined the button xml as follows: 在我的main.xml文件中,我已经定义了按钮xml,如下所示:

<Button
android:id="@+id/btnOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display Message"
android:onClick="onBtnClicked" />

I read on one of the posts that I need to register the onClick event in the XML layout. 我在其中一篇文章中读到了我需要在XML布局中注册onClick事件。 So that is what I thought I did in the XML code above. 这就是我认为我在上面的XML代码中所做的。 Then, in my java code file, I have written the following code: 然后,在我的java代码文件中,我编写了以下代码:

package com.example.helloandroid;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class HelloAndroid extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
    }   

    public void onBtnClicked(View v)
    { 
        if(v.getId() == R.id.btnOK)
        {
            MessageBox("Hello World");
        }       
    }

    public void MessageBox(String message)
    {
       Toast.makeText(this, message, Toast.LENGTH_SHORT);
    }   
}

To me, this makes sense. 对我而言,这是有道理的。 But the message box does not display when I click on the button. 但是,当我单击按钮时,不会显示消息框。 From the code imports above, you can see that I have already tried a few solutions without success. 从上面的代码导入中,您可以看到我已经尝试了一些解决方案而没有成功。 Am I perhaps missing a listener? 我可能错过了听众吗? I thought that the definition in the XML code would create this for me? 我认为XML代码中的定义会为我创建这个吗?

Thanks in advance :-) 提前致谢 :-)

Change 更改

Toast.makeText(this, message, Toast.LENGTH_SHORT);

To

Toast.makeText(this, message, Toast.LENGTH_SHORT).show();

The show() makes sure you actually display the Toast, else you are only creating the Toast. show()确保你实际显示Toast,否则你只是创建Toast。

Toast.makeText(this, message, Toast.LENGTH_SHORT);

这是对的

Toast.makeText(this, message, Toast.LENGTH_SHORT).show();

Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); - you need to call the show() method as now you are just creating the toast without showing it. - 你需要调用show()方法,因为现在你只是创建吐司而不显示它。

the Activity have to implement a View.OnClickListener, and implement the method onClick(View v) Activity必须实现一个View.OnClickListener,并实现onClick方法(View v)

in onCreate method, you have initialize the button (after the instruction setContentView): 在onCreate方法中,您已初始化按钮(在指令setContentView之后):

Button b = (Button) findViewById(R.id.btnOK);
b.setOnClickListener(this);

in onClick method: 在onClick方法中:

public void onClick(View v) {
    switch(v.getId()){
    case R.id.btnOK:
        /*  the instruccions of the button */
        break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM