android - 为什么我的 Toast 不显示,android 编程

标签 android android-studio toast

我编写了代码,当单击特定的单选按钮时会显示特定消息的 toast,但是当我单击时为什么没有弹出 toast? 为什么 toast 没有出现? 我编写了代码,当单击特定的单选按钮时会显示特定消息的 toast,但是当我单击时为什么没有弹出 toast? 为什么 toast 没有出现?

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
int id;
String text = "radio button 1!";
String text2="radioi button 2";
String text3="radio button 3";

int duration = Toast.LENGTH_SHORT;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    radioGroup = (RadioGroup) findViewById(R.id.radio_group);
    id = radioGroup.getCheckedRadioButtonId();
}
public void onRadioButtonClicked(View view) {


    switch(id) {
        case R.id.radioButton:
            Toast toast = Toast.makeText(this, text, duration);
            toast.show();

            break;
        case R.id.radioButton2:
            Toast toast2 = Toast.makeText(this, text2, duration);
            toast2.show();

            break;
        case R.id.radioButton3:
            Toast toast3 = Toast.makeText(this, text3, duration);
            toast3.show();
            default:


    }
}
}

这是xml布局代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ai_agamcompaq.radio.MainActivity">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radio_group">
<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New RadioButton"
    android:id="@+id/radioButton"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:onClick="onRadioButtonClicked"
    android:layout_marginTop="115dp" />

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New RadioButton"
    android:id="@+id/radioButton2"
    android:layout_below="@+id/radioButton"
    android:layout_alignStart="@+id/radioButton"
    android:onClick="onRadioButtonClicked"
    android:layout_marginTop="28dp" />

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New RadioButton"
    android:id="@+id/radioButton3"
    android:layout_below="@+id/radioButton2"
    android:layout_alignStart="@+id/radioButton2"
    android:onClick="onRadioButtonClicked"
    android:layout_marginTop="52dp" />


</RadioGroup>
</RelativeLayout>

最佳答案

您的switch 语句不正确。您永远不会看到选中的框,因为您已经要求选中的人。

public void onRadioButtonClicked(View view) {

switch(view.getId()) {
    case R.id.radioButton:
        Toast toast = Toast.makeText(this, text, duration);
        toast.show();
        break;
    case R.id.radioButton2:
        Toast toast2 = Toast.makeText(this, text2, duration);
        toast2.show();
        break;
    case R.id.radioButton3:
        Toast toast3 = Toast.makeText(this, text3, duration);
        toast3.show();
        default:
  }
}

关于android - 为什么我的 Toast 不显示,android 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42905088/

相关文章:

android studio VCS本地历史太短

android-studio - Android Studio gradle bintray 上传失败 : Could not sign version

gradle - android studio:gradle:构建永无止境,无法建立环回连接

Android 在点击时取消 toast

Android webview 无法加载 html 文本

java - Android SQLite 使用数组更新列

android - 将 Android Toast 持续时间设置得非常长(例如 1 分钟)

android - 可以指定 Toast 的位置吗?

android - 将 36 MB JSON 文件写入 Realm 数据库时 UI 被阻塞,因此无法显示进度

android - 加快 Android 动态 View 创建速度