我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
当前回答
LayoutInflater inflater = LayoutInflater.from(HistoryActivity.this);
final View vv = inflater.inflate(R.layout.dialog_processing_tts, null);
final AlertDialog.Builder alert = new AlertDialog.Builder(
HistoryActivity.this);
alert.setTitle("Delete");
alert.setView(vv);
alert.setCancelable(false)
.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
databaseHelperClass.deleteHistory(list.get(position).getID());
list.clear();
setAdapterForList();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
final AlertDialog dialog = alert.create();
dialog.show();
其他回答
public void showSimpleDialog(View view) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(false);
builder.setTitle("AlertDialog Title");
builder.setMessage("Simple Dialog Message");
builder.setPositiveButton("OK!!!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//
}
})
.setNegativeButton("Cancel ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
// Create the AlertDialog object and return it
builder.create().show();
}
也可以看看我的关于Android对话的博客,你会在这里找到所有的细节:http://www.fahmapps.com/2016/09/26/dialogs-in-android-part1/。
带有编辑文本的警报对话框
AlertDialog.Builder builder = new AlertDialog.Builder(context);//Context is activity context
final EditText input = new EditText(context);
builder.setTitle(getString(R.string.remove_item_dialog_title));
builder.setMessage(getString(R.string.dialog_message_remove_item));
builder.setTitle(getString(R.string.update_qty));
builder.setMessage("");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
input.setHint(getString(R.string.enter_qty));
input.setTextColor(ContextCompat.getColor(context, R.color.textColor));
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setText("String in edit text you want");
builder.setView(input);
builder.setPositiveButton(getString(android.R.string.ok),
(dialog, which) -> {
//Positive button click event
});
builder.setNegativeButton(getString(android.R.string.cancel),
(dialog, which) -> {
//Negative button click event
});
AlertDialog dialog = builder.create();
dialog.show();
我在onClick方法中使用这个AlertDialog:
button.setOnClickListener(v -> {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(this);
View view = layoutInflaterAndroid.inflate(R.layout.cancel_dialog, null);
builder.setView(view);
builder.setCancelable(false);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
view.findViewById(R.id.yesButton).setOnClickListener(v -> onBackPressed());
view.findViewById(R.id.nobutton).setOnClickListener(v -> alertDialog.dismiss());
});
dialog.xml
<androidx.constraintlayout.widget.ConstraintLayout 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:orientation="vertical">
<TextView
android:id="@+id/textmain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center"
android:padding="5dp"
android:text="@string/warning"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textpart2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center"
android:lines="2"
android:maxLines="2"
android:padding="5dp"
android:singleLine="false"
android:text="@string/dialog_cancel"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textmain" />
<TextView
android:id="@+id/yesButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="40dp"
android:layout_marginBottom="5dp"
android:background="#87cefa"
android:gravity="center"
android:padding="10dp"
android:text="@string/yes"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textpart2" />
<TextView
android:id="@+id/nobutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="40dp"
android:background="#87cefa"
android:gravity="center"
android:padding="10dp"
android:text="@string/no"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/yesButton" />
<TextView
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_margin="5dp"
android:padding="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nobutton" />
</androidx.constraintlayout.widget.ConstraintLayout>
new AlertDialog.Builder(v.getContext()).setMessage("msg to display!").show();
试试下面的代码:
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setMessage("Write your message here.");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder1.setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();