isChecked 函数不适用于 android 中的单选按钮
isChecked function is not working for radio button in android
下面是我的activity_main.xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/rgr">
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Mechanical" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Electrical" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Civil" />
<RadioButton
android:id="@+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Computer Science" />
</RadioGroup>
</LinearLayout>
下面是MainActivity.java代码
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{ // https://www.youtube.com/watch?v=n7c3bIWcgZo
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rgr);
RadioButton radioButton1 = (RadioButton)findViewById(R.id.rb1);
RadioButton radioButton2 = (RadioButton)findViewById(R.id.rb2);
RadioButton radioButton3 = (RadioButton)findViewById(R.id.rb3);
RadioButton radioButton4 = (RadioButton)findViewById(R.id.rb4);
if(radioButton1.isChecked())
{
Toast.makeText(getApplicationContext(), radioButton1.getText(),Toast.LENGTH_LONG).show();
}
else if(radioButton2.isChecked())
{
Toast.makeText(getApplicationContext(), radioButton2.getText(),Toast.LENGTH_LONG).show();
}
else if(radioButton3.isChecked())
{
Toast.makeText(getApplicationContext(), radioButton3.getText(),Toast.LENGTH_LONG).show();
}
else if(radioButton4.isChecked())
{
Toast.makeText(getApplicationContext(), radioButton4.getText(),Toast.LENGTH_LONG).show();
}
}
现在,当我 运行 应用程序时,单选按钮正在 selected,但是当我 select 特定按钮时,toast 消息没有出现。
我也尝试过删除单选组并只保留单选按钮,但我仍然认为 isChecked() 函数没有执行。
请帮我解决这个问题。
您需要为 radioGroup 设置 OnCheckedChangeListener 以查找选中或未选中哪个单选按钮。像下面这样
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
RadioButton radioButton = (RadioButton) findViewById(checkedId);
Toast.makeText(getBaseContext(), radioButton.getText(), Toast.LENGTH_SHORT).show();
}
});
您必须为您的单选按钮设置 listener
,如下所示。
radioButton1 .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked)
Toast.makeText(getApplicationContext(), radioButton1.getText(),Toast.LENGTH_LONG).show();
}
}
);
下面是我的activity_main.xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/rgr">
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Mechanical" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Electrical" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Civil" />
<RadioButton
android:id="@+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Computer Science" />
</RadioGroup>
</LinearLayout>
下面是MainActivity.java代码
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{ // https://www.youtube.com/watch?v=n7c3bIWcgZo
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rgr);
RadioButton radioButton1 = (RadioButton)findViewById(R.id.rb1);
RadioButton radioButton2 = (RadioButton)findViewById(R.id.rb2);
RadioButton radioButton3 = (RadioButton)findViewById(R.id.rb3);
RadioButton radioButton4 = (RadioButton)findViewById(R.id.rb4);
if(radioButton1.isChecked())
{
Toast.makeText(getApplicationContext(), radioButton1.getText(),Toast.LENGTH_LONG).show();
}
else if(radioButton2.isChecked())
{
Toast.makeText(getApplicationContext(), radioButton2.getText(),Toast.LENGTH_LONG).show();
}
else if(radioButton3.isChecked())
{
Toast.makeText(getApplicationContext(), radioButton3.getText(),Toast.LENGTH_LONG).show();
}
else if(radioButton4.isChecked())
{
Toast.makeText(getApplicationContext(), radioButton4.getText(),Toast.LENGTH_LONG).show();
}
}
现在,当我 运行 应用程序时,单选按钮正在 selected,但是当我 select 特定按钮时,toast 消息没有出现。
我也尝试过删除单选组并只保留单选按钮,但我仍然认为 isChecked() 函数没有执行。
请帮我解决这个问题。
您需要为 radioGroup 设置 OnCheckedChangeListener 以查找选中或未选中哪个单选按钮。像下面这样
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
RadioButton radioButton = (RadioButton) findViewById(checkedId);
Toast.makeText(getBaseContext(), radioButton.getText(), Toast.LENGTH_SHORT).show();
}
});
您必须为您的单选按钮设置 listener
,如下所示。
radioButton1 .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked)
Toast.makeText(getApplicationContext(), radioButton1.getText(),Toast.LENGTH_LONG).show();
}
}
);