0

我在我的 Android 项目中创建了一个按钮,但它不起作用。这是我的应用程序(基于“hello world”):

activity_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, I am a TextView" />

    <Button android:id="@+id/button_roy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, I am a Button" />

</LinearLayout>

这是我的 java roy.java

package aa.aa.aaaaaaa.royb;

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

/**
 * Created by royb on 05/11/13.
 */
public class roy  extends Activity {
    public void onCreate (Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button createBtn = (Button) findViewById(R.id.button_roy);
        createBtn.setOnClickListener(new View.OnClickListener()
        {
            public void onClick (View v)
            {
                Context context = getApplicationContext();
                CharSequence text = "Hello toast!";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
        });
    }
}

当我在我的 android 设备上运行它并单击按钮时,它什么也不做。如何使吐司显示?

4

1 回答 1

0
public class Roy  extends Activity
{ 
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         Button createBtn = (Button) findViewById(R.id.button_roy);
        createBtn.setOnClickListener(new View.OnClickListener()
        {
            public void onClick (View v)
            {
                   Toast.makeText(getApplicationContext,"HelloToast",Toast.LENGTH_SHORT).show();
            }
        }
}
于 2015-01-24T08:47:02.690 回答