android - 在 Android 应用中切换大小写

标签 android switch-statement case

这是当我在我的最小 Activity 页面中按下“关于”按钮时应该打开一个对话框的代码。但什么都没有发生。 :/

@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;  


        }       
    }

主要 Activity 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>

按钮示例:关于

关于 Activity

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>

最佳答案

这是你的问题:

@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 语句中也有一个 default case,如果没有任何 case 成立,则执行该 default case。添加带有日志消息或其他内容的默认情况,然后查看它是否被执行。

关于android - 在 Android 应用中切换大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19950611/

相关文章:

javascript - switch 中的函数声明 - 为什么只保留最后一个定义?

sql - 基于两个表的select CASE语句

java - 从浏览器重定向到 Android 应用

arrays - Swift - 使用 Switch 更改 UIButton 的功能

android - 在 Android 上以编程方式安装应用程序

android - 使用sharedPreferences在Flutter中保存开关状态

sql-server - SQL SUM(QTY) 但仅适用于 QTY > 0 的记录

mysql - 使用 MySQL Case 比较日期

Android,如何停止指针出现在 EditText 下方

Java - 将字符串拆分为具有字符限制的句子