我有一个对话框。我想更改选中的属性,但不能。我想选中是否选中单选按钮。
它是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单选按钮下面的线性布局,但它崩溃了。 我认为出现对话框后无法更改对话框的视图。否则我做不对。
答案 0 :(得分:2)
欢迎使用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);
}
}
});
}
});
根据您的需要将需要一些调整。祝你好运!