Android 单选按钮未选中
Android Radio Button Not Checked
我正在 xml 文件中添加三个单选按钮。每个单选按钮都在单独的 LinearLayout 中,但相同 xml。首先,我不知道如何将它们添加到无线电组,同时将它们保持在单独的 LinearLayouts 中。其次,我编写了以下代码来仅检查一个单选按钮。但这是行不通的。请让我知道我应该如何进行以及是否有更好的解决方案。这是代码。
RadioButton screenof;
RadioButton screenof3;
RadioButton onexit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
screenof = (RadioButton)findViewById(R.id.radioscreenof);
screenof.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!screenof.isChecked())
{
screenof.setChecked(true);
screenof3.setChecked(false);
onexit.setChecked(false);
}
}
});
screenof3 = (RadioButton)findViewById(R.id.radioscreenof3);
screenof3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!screenof3.isChecked())
{
screenof3.setChecked(true);
screenof.setChecked(false);
onexit.setChecked(false);
}
}
});
onexit = (RadioButton)findViewById(R.id.radiolockimmediately);
onexit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!onexit.isChecked())
{
screenof3.setChecked(false);
screenof.setChecked(false);
onexit.setChecked(true);
}
}
});
}
这是 xml 文件
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:background="@color/widgetcolor"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="1dp"
android:background="@color/deactiveTexts"
android:clickable="false"
android:layout_weight="1"
>
<TextView
android:text="Lock Mode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17dp"
android:layout_weight="1"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="1dp"
android:background="@color/colorAccent"
android:layout_weight="1"
android:id="@+id/lockscreenof"
>
<TextView
android:text="Lock apps at screen off"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:textSize="17dp"
android:layout_weight="1"
android:textStyle="bold" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioscreenof"
android:layout_weight="1"
android:paddingRight="30dp"
app:buttonTint="@android:color/holo_green_dark"
android:elevation="0dp"
android:duplicateParentState="false"
android:checked="false"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="1dp"
android:background="@color/colorAccent"
android:layout_weight="1"
android:id="@+id/lockscreenof3"
>
<TextView
android:text="Lock apps after screen of for 3 minutes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView3"
android:textSize="17dp"
android:layout_weight="1"
android:textStyle="bold" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioscreenof3"
android:layout_weight="1"
android:paddingRight="30dp"
app:buttonTint="@android:color/holo_green_dark" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="1dp"
android:background="@color/colorAccent"
android:layout_weight="1"
android:id="@+id/lockimmediately"
>
<TextView
android:text="Lock apps immediately upon exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:textSize="17dp"
android:layout_weight="1"
android:textStyle="bold" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radiolockimmediately"
android:layout_weight="1"
android:paddingRight="30dp"
app:buttonTint="@android:color/holo_green_dark" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="1dp"
android:background="@color/colorAccent"
android:id="@+id/passonce"
android:layout_weight="1"
>
<TextView
android:text="Enter password once to unlock all apps"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView4"
android:textSize="17dp"
android:layout_weight="1"
android:textStyle="bold" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkpassonce"
android:layout_weight="1"
android:paddingRight="30dp"
app:buttonTint="@android:color/holo_green_dark" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="1dp"
android:background="@color/colorAccent"
android:id="@+id/displayhint"
android:layout_weight="1"
>
<TextView
android:text="Display lock mode hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView5"
android:textSize="17dp"
android:layout_weight="1"
android:textStyle="bold" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkhint"
android:layout_weight="1"
android:paddingRight="30dp"
app:buttonTint="@android:color/holo_green_dark"
android:checked="false" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="1dp"
android:background="@color/deactiveTexts"
android:clickable="false"
android:layout_weight="1"
>
<TextView
android:text="Password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView6"
android:textSize="17dp"
android:layout_weight="1"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="1dp"
android:background="@color/colorAccent"
android:clickable="true"
android:id="@+id/changepass"
android:layout_weight="1"
>
<TextView
android:text="Change password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17dp"
android:layout_weight="1"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="1dp"
android:background="@color/colorAccent"
android:id="@+id/invisiblepattern"
android:layout_weight="1"
>
<TextView
android:text="Invisible Pattern"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17dp"
android:layout_weight="1"
android:textStyle="bold" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkpatinvisible"
android:layout_weight="1"
android:paddingRight="30dp"
app:buttonTint="@android:color/holo_green_dark" />
</LinearLayout>
</LinearLayout>
我编辑了代码并粘贴了您的代码,但是当我 运行 代码出现意外结果时。我放了打印语句,这是它打印的内容。我从上到下单击了一次按钮。
Case 1
Case 1
Case 2
Case 2
Case 1
Case 2
Case 3
您可以通过实施 OnCheckedChangeListener
来做到这一点
试试下面的代码它会为你工作-
screenof= (RadioButton) findViewById(R.id.radioscreenof);
screenof.setOnCheckedChangeListener(quest1);
screenof3= (RadioButton) findViewById(R.id.radioscreenof3);
screenof3.setOnCheckedChangeListener(quest1);
onExit= (RadioButton) findViewById(R.id.radiolockimmediately);
onExit.setOnCheckedChangeListener(quest1);
CompoundButton.OnCheckedChangeListener quest1 = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.radioscreenof:
radioscreenof.setChecked(true);
radioscreenof3.setChecked(false);
onnExit.setChecked(false);
break;
case R.id.radioscreenof3:
radioscreenof.setChecked(false);
radioscreenof3.setChecked(true);
onnExit.setChecked(false);
break;
case R.id.onExit:
radioscreenof.setChecked(false);
radioscreenof3.setChecked(false);
onExit.setChecked(true);
}
}
};
使用此布局:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="left"
android:layout_margin="25dp"
android:background="#FF0"
android:paddingRight="50dp"
>
<RadioButton
android:id="@+id/radioscreenof"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lock apps at screen off"
android:onClick="onRadioButtonClicked"
/>
<RadioButton
android:id="@+id/radioscreenof3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lock apps after screen of for 3 minutes"
android:onClick="onRadioButtonClicked"
/>
<RadioButton
android:id="@+id/radiolockimmediately"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lock apps immediately upon exit"
android:onClick="onRadioButtonClicked"
/>
</RadioGroup>
然后对于点击侦听器首先清除无线电组的所有检查
//reference radiogroup ID from layout file
radiogroup.clearCheck();
为广播组设置一个 checkedchange 侦听器
radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Log.d("chk", "id" + checkedId);
if (checkedId == R.id.a) {
//R.id.a = RadioButton ID in layout
//some code
} else if (checkedId == R.id.b) {
//some code
}
}
});
您也可以通过编程方式触发 radioButton 上的侦听器..
rb = (RadioButton) view.findViewById(R.id.a);
rb.setChecked(true);
试试这个:
screenof= (RadioButton) findViewById(R.id.radioscreenof);
screenof.setOnCheckedChangeListener(quest1);
screenof3= (RadioButton) findViewById(R.id.radioscreenof3);
screenof3.setOnCheckedChangeListener(quest1);
onExit= (RadioButton) findViewById(R.id.radiolockimmediately);
onExit.setOnCheckedChangeListener(quest1);
CompoundButton.OnCheckedChangeListener quest1 = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.radioscreenof:
radioscreenof.setChecked(true);
radioscreenof3.setChecked(false);
onnExit.setChecked(false);
break;
case R.id.radioscreenof3:
radioscreenof.setChecked(false);
radioscreenof3.setChecked(true);
onnExit.setChecked(false);
break;
case R.id.radiolockimmediately:
radioscreenof.setChecked(false);
radioscreenof3.setChecked(false);
onExit.setChecked(true);
}
}
};
这是@Amit 的回答我刚刚在 中用 R.id.radiolockimmediately 替换了 R.id.onExit onCheckedChanged
(我无法发表评论,所以我不得不 post 一个新的答案)
我正在 xml 文件中添加三个单选按钮。每个单选按钮都在单独的 LinearLayout 中,但相同 xml。首先,我不知道如何将它们添加到无线电组,同时将它们保持在单独的 LinearLayouts 中。其次,我编写了以下代码来仅检查一个单选按钮。但这是行不通的。请让我知道我应该如何进行以及是否有更好的解决方案。这是代码。
RadioButton screenof;
RadioButton screenof3;
RadioButton onexit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
screenof = (RadioButton)findViewById(R.id.radioscreenof);
screenof.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!screenof.isChecked())
{
screenof.setChecked(true);
screenof3.setChecked(false);
onexit.setChecked(false);
}
}
});
screenof3 = (RadioButton)findViewById(R.id.radioscreenof3);
screenof3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!screenof3.isChecked())
{
screenof3.setChecked(true);
screenof.setChecked(false);
onexit.setChecked(false);
}
}
});
onexit = (RadioButton)findViewById(R.id.radiolockimmediately);
onexit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!onexit.isChecked())
{
screenof3.setChecked(false);
screenof.setChecked(false);
onexit.setChecked(true);
}
}
});
}
这是 xml 文件
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:background="@color/widgetcolor"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="1dp"
android:background="@color/deactiveTexts"
android:clickable="false"
android:layout_weight="1"
>
<TextView
android:text="Lock Mode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17dp"
android:layout_weight="1"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="1dp"
android:background="@color/colorAccent"
android:layout_weight="1"
android:id="@+id/lockscreenof"
>
<TextView
android:text="Lock apps at screen off"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:textSize="17dp"
android:layout_weight="1"
android:textStyle="bold" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioscreenof"
android:layout_weight="1"
android:paddingRight="30dp"
app:buttonTint="@android:color/holo_green_dark"
android:elevation="0dp"
android:duplicateParentState="false"
android:checked="false"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="1dp"
android:background="@color/colorAccent"
android:layout_weight="1"
android:id="@+id/lockscreenof3"
>
<TextView
android:text="Lock apps after screen of for 3 minutes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView3"
android:textSize="17dp"
android:layout_weight="1"
android:textStyle="bold" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioscreenof3"
android:layout_weight="1"
android:paddingRight="30dp"
app:buttonTint="@android:color/holo_green_dark" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="1dp"
android:background="@color/colorAccent"
android:layout_weight="1"
android:id="@+id/lockimmediately"
>
<TextView
android:text="Lock apps immediately upon exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:textSize="17dp"
android:layout_weight="1"
android:textStyle="bold" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radiolockimmediately"
android:layout_weight="1"
android:paddingRight="30dp"
app:buttonTint="@android:color/holo_green_dark" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="1dp"
android:background="@color/colorAccent"
android:id="@+id/passonce"
android:layout_weight="1"
>
<TextView
android:text="Enter password once to unlock all apps"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView4"
android:textSize="17dp"
android:layout_weight="1"
android:textStyle="bold" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkpassonce"
android:layout_weight="1"
android:paddingRight="30dp"
app:buttonTint="@android:color/holo_green_dark" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="1dp"
android:background="@color/colorAccent"
android:id="@+id/displayhint"
android:layout_weight="1"
>
<TextView
android:text="Display lock mode hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView5"
android:textSize="17dp"
android:layout_weight="1"
android:textStyle="bold" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkhint"
android:layout_weight="1"
android:paddingRight="30dp"
app:buttonTint="@android:color/holo_green_dark"
android:checked="false" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="1dp"
android:background="@color/deactiveTexts"
android:clickable="false"
android:layout_weight="1"
>
<TextView
android:text="Password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView6"
android:textSize="17dp"
android:layout_weight="1"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="1dp"
android:background="@color/colorAccent"
android:clickable="true"
android:id="@+id/changepass"
android:layout_weight="1"
>
<TextView
android:text="Change password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17dp"
android:layout_weight="1"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="1dp"
android:background="@color/colorAccent"
android:id="@+id/invisiblepattern"
android:layout_weight="1"
>
<TextView
android:text="Invisible Pattern"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17dp"
android:layout_weight="1"
android:textStyle="bold" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkpatinvisible"
android:layout_weight="1"
android:paddingRight="30dp"
app:buttonTint="@android:color/holo_green_dark" />
</LinearLayout>
</LinearLayout>
我编辑了代码并粘贴了您的代码,但是当我 运行 代码出现意外结果时。我放了打印语句,这是它打印的内容。我从上到下单击了一次按钮。
Case 1
Case 1
Case 2
Case 2
Case 1
Case 2
Case 3
您可以通过实施 OnCheckedChangeListener
来做到这一点试试下面的代码它会为你工作-
screenof= (RadioButton) findViewById(R.id.radioscreenof);
screenof.setOnCheckedChangeListener(quest1);
screenof3= (RadioButton) findViewById(R.id.radioscreenof3);
screenof3.setOnCheckedChangeListener(quest1);
onExit= (RadioButton) findViewById(R.id.radiolockimmediately);
onExit.setOnCheckedChangeListener(quest1);
CompoundButton.OnCheckedChangeListener quest1 = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.radioscreenof:
radioscreenof.setChecked(true);
radioscreenof3.setChecked(false);
onnExit.setChecked(false);
break;
case R.id.radioscreenof3:
radioscreenof.setChecked(false);
radioscreenof3.setChecked(true);
onnExit.setChecked(false);
break;
case R.id.onExit:
radioscreenof.setChecked(false);
radioscreenof3.setChecked(false);
onExit.setChecked(true);
}
}
};
使用此布局:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="left"
android:layout_margin="25dp"
android:background="#FF0"
android:paddingRight="50dp"
>
<RadioButton
android:id="@+id/radioscreenof"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lock apps at screen off"
android:onClick="onRadioButtonClicked"
/>
<RadioButton
android:id="@+id/radioscreenof3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lock apps after screen of for 3 minutes"
android:onClick="onRadioButtonClicked"
/>
<RadioButton
android:id="@+id/radiolockimmediately"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lock apps immediately upon exit"
android:onClick="onRadioButtonClicked"
/>
</RadioGroup>
然后对于点击侦听器首先清除无线电组的所有检查
//reference radiogroup ID from layout file
radiogroup.clearCheck();
为广播组设置一个 checkedchange 侦听器
radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Log.d("chk", "id" + checkedId);
if (checkedId == R.id.a) {
//R.id.a = RadioButton ID in layout
//some code
} else if (checkedId == R.id.b) {
//some code
}
}
});
您也可以通过编程方式触发 radioButton 上的侦听器..
rb = (RadioButton) view.findViewById(R.id.a);
rb.setChecked(true);
试试这个:
screenof= (RadioButton) findViewById(R.id.radioscreenof);
screenof.setOnCheckedChangeListener(quest1);
screenof3= (RadioButton) findViewById(R.id.radioscreenof3);
screenof3.setOnCheckedChangeListener(quest1);
onExit= (RadioButton) findViewById(R.id.radiolockimmediately);
onExit.setOnCheckedChangeListener(quest1);
CompoundButton.OnCheckedChangeListener quest1 = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.radioscreenof:
radioscreenof.setChecked(true);
radioscreenof3.setChecked(false);
onnExit.setChecked(false);
break;
case R.id.radioscreenof3:
radioscreenof.setChecked(false);
radioscreenof3.setChecked(true);
onnExit.setChecked(false);
break;
case R.id.radiolockimmediately:
radioscreenof.setChecked(false);
radioscreenof3.setChecked(false);
onExit.setChecked(true);
}
}
};
这是@Amit 的回答我刚刚在 中用 R.id.radiolockimmediately 替换了 R.id.onExit onCheckedChanged
(我无法发表评论,所以我不得不 post 一个新的答案)