如何在 Android 中创建这种类型的弹出对话框?
How to create this type of popup dialogue in Android?
这张图片来自 Play 商店的 Deep Relax 应用:
如何创建弹出菜单?我正在创建一个应用我想请用户提供反馈?
为此目的使用自定义对话框试试这个:
像这样创建布局custom_dialog_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="title"
android:textColor="#000" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting," />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="description" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:id="@+id/btnNotNOw"
android:text="not now"
android:textColor="#040d28" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@null"
android:text="Never"
android:id="@+id/btnNever"
android:textColor="#040d28" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@null"
android:text="sure"
android:id="@+id/btnSure"
android:textColor="#040d28" />
</LinearLayout>
</LinearLayout>
现在使用以下代码创建对话框:
final Dialog dialog = new Dialog(LoginActivity.this);
dialog.setContentView(R.layout.custom_dialog_layout);
Button btnNotNOw, btnNever, btnSure;
btnNotNOw = (Button) dialog.findViewById(R.id.btnNotNOw);
btnNever = (Button) dialog.findViewById(R.id.btnNever);
btnSure = (Button) dialog.findViewById(R.id.btnSure);
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.BOTTOM);
dialog.show();
btnNever.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// perform your action here
}
});
btnNotNOw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// perform your action here
}
});
btnSure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// perform your action here
}
});
您可以通过自定义对话框创建它:
自定义对话框:
为此您必须创建一个 xml
文件并附上。
final Dialog dialog1 = new Dialog(LabCheckOutActivity.this);
dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog1.setCancelable(true);
dialog1.setContentView(R.layout.dialog_patient_details);
dialog1.show();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Top Heading");
// Setting Dialog Message
alertDialog.setMessage("Message");
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Sure", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed YES button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on Sure",
Toast.LENGTH_SHORT).show();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("Not Now", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed No button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on Not Now", Toast.LENGTH_SHORT).show();
}
});
// Setting Netural "Cancel" Button
alertDialog.setNeutralButton("Never", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed Cancel button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on Never",
Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
检查这个
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this, AlertDialog.THEME_TRADITIONAL);
builder.setMessage("Positive,Negative and Neutral Button")
builder.setCancelable(false);
builder.setTitle("Custom Dialog");
// Set the positive/yes button click listener
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
// Set the negative/no button click listener
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
// Set the neutral/cancel button click listener
builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
// Display the alert dialog on interface
dialog.show();
这张图片来自 Play 商店的 Deep Relax 应用:
如何创建弹出菜单?我正在创建一个应用我想请用户提供反馈?
为此目的使用自定义对话框试试这个:
像这样创建布局custom_dialog_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="title"
android:textColor="#000" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting," />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="description" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:id="@+id/btnNotNOw"
android:text="not now"
android:textColor="#040d28" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@null"
android:text="Never"
android:id="@+id/btnNever"
android:textColor="#040d28" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@null"
android:text="sure"
android:id="@+id/btnSure"
android:textColor="#040d28" />
</LinearLayout>
</LinearLayout>
现在使用以下代码创建对话框:
final Dialog dialog = new Dialog(LoginActivity.this);
dialog.setContentView(R.layout.custom_dialog_layout);
Button btnNotNOw, btnNever, btnSure;
btnNotNOw = (Button) dialog.findViewById(R.id.btnNotNOw);
btnNever = (Button) dialog.findViewById(R.id.btnNever);
btnSure = (Button) dialog.findViewById(R.id.btnSure);
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.BOTTOM);
dialog.show();
btnNever.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// perform your action here
}
});
btnNotNOw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// perform your action here
}
});
btnSure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// perform your action here
}
});
您可以通过自定义对话框创建它:
自定义对话框:
为此您必须创建一个 xml
文件并附上。
final Dialog dialog1 = new Dialog(LabCheckOutActivity.this);
dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog1.setCancelable(true);
dialog1.setContentView(R.layout.dialog_patient_details);
dialog1.show();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Top Heading");
// Setting Dialog Message
alertDialog.setMessage("Message");
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Sure", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed YES button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on Sure",
Toast.LENGTH_SHORT).show();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("Not Now", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed No button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on Not Now", Toast.LENGTH_SHORT).show();
}
});
// Setting Netural "Cancel" Button
alertDialog.setNeutralButton("Never", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed Cancel button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on Never",
Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
检查这个
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this, AlertDialog.THEME_TRADITIONAL);
builder.setMessage("Positive,Negative and Neutral Button")
builder.setCancelable(false);
builder.setTitle("Custom Dialog");
// Set the positive/yes button click listener
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
// Set the negative/no button click listener
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
// Set the neutral/cancel button click listener
builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
// Display the alert dialog on interface
dialog.show();