1

这是当我在我的最小活动页面中按下关于按钮时应该打开一个对话框的代码。但实际上什么也没发生。:/

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        View continueButton = findViewById(R.id.continue_button);
        continueButton.setOnClickListener(this);

        View newgameButton = findViewById(R.id.new_button);
        continueButton.setOnClickListener(this);

        View aboutButton = findViewById(R.id.about_button);
        continueButton.setOnClickListener(this);

        View exitButton = findViewById(R.id.exit_button);
        continueButton.setOnClickListener(this);




public void onClick(View v) {

        switch (v.getId()) {
        case R.id.continue_button:
                break;
        case R.id.about_button:     
                Intent i = new Intent(this, About.class);
                startActivity(i);
                break;
        case R.id.new_button: 
                openNewGameDialog(); 
                break;
        case R.id.exit_button:
                finish();
                break;  


        }       
    }

主要活动 XML

<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:background="@color/background"
   android:layout_height="fill_parent"
   android:layout_width="fill_parent"
   android:padding="30dip"
   android:orientation="horizontal" >
<LinearLayout
   android:orientation="vertical"
   android:layout_height="wrap_content"
   android:layout_width="fill_parent"
   android:layout_gravity="center" >
 <TextView
   android:text="@string/main_title"
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:layout_gravity="center"
   android:layout_marginBottom="25dip"
   android:textSize="24.5sp" />
 <Button
   android:id="@+id/continue_button"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/continue_label" />
 <Button
   android:id="@+id/new_button"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/new_game_label" />
 <Button
   android:id="@+id/about_button"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/about_label" />
 <Button
   android:id="@+id/exit_button"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/exit_label" />
</LinearLayout>
</LinearLayout>

按钮示例:关于

关于活动

public class About extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.about, menu);
        return true;
    }

}

关于 XML

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip" >
<TextView
android:id="@+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/about_text" />
</ScrollView>
4

3 回答 3

3

这是你的问题:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    View continueButton = findViewById(R.id.continue_button);
    continueButton.setOnClickListener(this);

    View newgameButton = findViewById(R.id.new_button);
    **newgameButton**.setOnClickListener(this);

    View aboutButton = findViewById(R.id.about_button);
    **aboutButton**.setOnClickListener(this);

    View exitButton = findViewById(R.id.exit_button);
    **exitButton**.setOnClickListener(this);
}

只有您的“continueButton”注册了 onClickListener...

另外:最好在 switch 语句中也有一个默认情况,如果没有任何情况成立,就会执行该情况。添加带有日志消息或其他内容的默认案例,看看它是否被执行。

于 2013-11-13T09:58:44.077 回答
1

首先,确保您的活动实现 View.OnClickListener 接口。

其次,尝试在 public void onClick(View view) { } 方法声明之前添加@Override。如果这没有帮助,请尝试在 onClick 方法的顶部添加一条日志语句,例如记录视图的 ID,并将其与您尝试与之匹配的那些进行比较。

于 2013-11-13T12:52:51.223 回答
0

据我所知,您需要调用button.setOnClickListener(this);您的活动 onCreate 方法

于 2013-11-13T09:57:39.923 回答