我尝试用Dialog
,2 x ImageView
,3 x TextView
和2x RadioButton
s创建自定义Button
。当然,我不能将其设置为常规的Dialog
(由于使用了View
种类繁多),所以我决定创建自定义layout
和一个Class
将负责全部设置。直到我构建它,一切似乎都很好。
对话框仅显示ImageView
和前TextView
,但其他View
不见了。
应该看起来像这样:
多数民众赞成在我的代码-布局:
<?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"
android:background="@color/dark_site_gray">
<Button
android:id="@+id/button4"
android:layout_width="172dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:background="@color/igloo_theme_color"
android:text="OK"
android:textColor="@color/mdtp_white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/radioButton4" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@color/burgrundy" />
<TextView
android:id="@+id/textView"
android:layout_width="327dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:text="TextView"
android:textColor="@color/mdtp_white"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:text="TextView"
android:textColor="@color/mdtp_white"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:buttonTint="@color/mdtp_white"
android:text="Akceptuj"
android:textColor="@color/mdtp_white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:buttonTint="@color/mdtp_white"
android:text="Odrzuć"
android:textColor="@color/mdtp_white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioButton" />
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:buttonTint="@color/mdtp_white"
android:text="Przekaż dalej"
android:textColor="@color/mdtp_white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioButton2" />
<Button
android:id="@+id/button"
android:layout_width="172dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:background="@color/igloo_theme_color"
android:text="Anuluj"
android:textColor="@color/mdtp_white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioButton4" />
</android.support.constraint.ConstraintLayout>
班级:
public class DialogCreator extends Dialog {
public DialogCreator(@NonNull Context context) {
super(context);
}
public void showSwitchDialog(Context context, String taskID, boolean isCoordinator, View.OnClickListener posButtonClickListener){
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog_switch_service);
TextView task = dialog.findViewById(R.id.textView);
TextView chooseOptionText = dialog.findViewById(R.id.textView2);
Button cancelButton = dialog.findViewById(R.id.button);
Button okButton = dialog.findViewById(R.id.button4);
RadioButton redirect = dialog.findViewById(R.id.radioButton4);
if(!isCoordinator) redirect.setVisibility(View.GONE);
okButton.setOnClickListener(posButtonClickListener);
task.setText("ID: " + taskID);
chooseOptionText.setText("Choose option:");
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
方法调用(对话框显示):
DialogCreator dc = new DialogCreator(MainScreenActivity.this);
dc.showSwitchDialog(MainScreenActivity.this, "000000", true, new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton radioAccept = v.findViewById(R.id.radioButton);
RadioButton radioDecline = v.findViewById(R.id.radioButton2);
RadioButton radioRedirect = v.findViewById(R.id.radioButton4);
if(radioAccept.isChecked()){
Toast.makeText(MainScreenActivity.this, "Accept", Toast.LENGTH_SHORT).show();
}
if(radioDecline.isChecked()){
Toast.makeText(MainScreenActivity.this, "Decline", Toast.LENGTH_SHORT).show();
}
if(radioRedirect.getVisibility() != View.GONE){
if (radioRedirect.isChecked()){
Toast.makeText(MainScreenActivity.this, "Redirect", Toast.LENGTH_SHORT).show();
}
}
}
});
一切似乎都很好(以我的愚见),但是我不知道是否存在某些错误或遗漏,或者我不能仅以此方式使用Dialog
。如果这是正确的方法,那么对我来说太好了,因为我可以用自定义版本替换以前使用过的所有Dialog
,这会使应用程序变得更好,更清晰。