[英]How to check checkbox if radio button is checked
我有一個對話框。 我想更改選中的屬性,但不能。 我想勾選單選按鈕。
它是activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="336dp"
android:text="TextView"
android:textSize="40sp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/start" />
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
是activity_dialog.xml提供了我的自定義對話框。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<RadioButton
android:id="@+id/singleA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:checked="false"
android:onClick="tytClick"
android:text="TYT" />
<LinearLayout
android:id="@+id/linearTYT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:orientation="vertical"
android:visibility="visible"
tools:context=".MainActivity">
<CheckBox
android:id="@+id/multi1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:checked="false"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Maths" />
<CheckBox
android:id="@+id/multi2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:checked="false"
android:text="Turkish"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"/>
<CheckBox
android:id="@+id/multi3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:checked="false"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="English" />
</LinearLayout>
<RadioButton
android:id="@+id/singleB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:checked="false"
android:onClick="aytClick"
android:text="AYT" />
</RadioGroup>
</android.support.constraint.ConstraintLayout>
並且是MainActivity.class
public class MainActivity extends AppCompatActivity {
final Context context = this;
Button start;
TextView textView;
CheckBox multi1, multi2,multi3;
RadioButton singleA, singleB;
LinearLayout linearTYT;
RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = findViewById(R.id.start);
textView = findViewById(R.id.textView);
multi1 = findViewById(R.id.multi1);
multi2 = findViewById(R.id.multi2);
multi3 = findViewById(R.id.multi3);
singleA = findViewById(R.id.singleA);
singleB = findViewById(R.id.singleB);
linearTYT = findViewById(R.id.linearTYT);
radioGroup = findViewById(R.id.radioGroup);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.activity_dialog);
dialog.setTitle("Choose");
dialog.show();
}
});
if (singleA.isChecked()) {
multi1.setChecked(true);
multi2.setChecked(true);
multi3.setChecked(true);
linearTYT.setVisibility(View.VISIBLE);
} else {
multi1.setChecked(false);
multi2.setChecked(false);
multi3.setChecked(false);
linearTYT.setVisibility(View.GONE);
}
}}
我還嘗試了View.Gone單選按鈕下面的線性布局,但是它崩潰了。 我認為出現對話框后無法更改對話框的視圖。 否則我做不對。
歡迎使用StackOverflow。
好像您是一個初學者。 我正在提供解決方案,但可能需要進行一些修改才能實現完美的功能。
您需要修改OnClickListener。
首先,您在對話框中使用activity_dialog.class(我相信是activity_dialog.xml
)。 因此,您根本無法通過Activity類的findViewById
方法獲取其視圖。 有一個setOnCheckChangeListener
方法可以實現所需的功能。 它類似於Button.setOnClickListener()
。
OnClickListener-
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.activity_dialog);
dialog.setTitle("Choose");
dialog.show();
multi1 = dialog.findViewById(R.id.multi1);
multi2 = dialog.findViewById(R.id.multi2);
multi3 = dialog.findViewById(R.id.multi3);
singleA = dialog.findViewById(R.id.singleA);
singleB = dialog.findViewById(R.id.singleB);
singleA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
multi1.setChecked(true);
multi2.setChecked(true);
multi3.setChecked(true);
}else{
multi1.setChecked(false);
multi2.setChecked(false);
multi3.setChecked(false);
}
}
});
singleB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
multi1.setChecked(false);
multi2.setChecked(false);
multi3.setChecked(false);
}else{
multi1.setChecked(true);
multi2.setChecked(true);
multi3.setChecked(true);
}
}
});
}
});
根據您的需要將需要一些調整。 祝好運!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.