Java Android AlertDialog,带有3个按钮的公共消息
请帮我写代码 我需要显示一个AndroidJava Android AlertDialog,带有3个按钮的公共消息,java,android,Java,Android,请帮我写代码 我需要显示一个AndroidAlertDialogBox,其中包含3个按钮的常见消息,即是、否和取消。在这里,我使用了简单的Android AlertDialog,其中包含一条消息,用于单独的3个按钮,即Yes、No和Cancel。 所以我认为需要调用函数 下面给出了布局和Java部分 布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http
AlertDialogBox
,其中包含3个按钮的常见消息,即是、否和取消。在这里,我使用了简单的Android AlertDialog,其中包含一条消息,用于单独的3个按钮,即Yes、No和Cancel。
所以我认为需要调用函数
下面给出了布局和Java部分
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.talent.simpledialogbox.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="/hello_world"
android:id="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes"
android:layout_above="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/Yes"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_above="@+id/textView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/Cancel"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No"
android:layout_above="@+id/textView"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView"
android:id="@+id/No"/>
</RelativeLayout>
分配
AlertDialog
对象并将其分配给builder.create()
不,它不起作用。当我使用alertDialog.show()单击这三个按钮时,我只需要得到一个“警报”;
package com.talent.simpledialogbox;
import android.os.Bundle;enter code here
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.annotation.RequiresPermission;
import android.view.Menu;
import android`enter code here`.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button Yes,No, Cancel;
Object myObject = new Object();
RequiresPermission.Read FunctionCalling;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Yes = (Button)findViewById(R.id.Yes);
No = (Button)findViewById(R.id.No);
Cancel= (Button)findViewById(R.id.Cancel);
Yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm Delete...");
builder.setMessage("Are you sure you want delete this?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
builder.show();
}
});
No.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm No)...");
builder.setMessage("Are you sure you want delete this?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
builder.show();
}
});
Cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm Cancel...");
builder.setMessage("Are you sure you want delete this?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on Cancel", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
// Showing Alert Message
builder.show();
}
});
}
}
package com.talent.simpledialogbox;
import android.os.Bundle;enter code here
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.annotation.RequiresPermission;
import android.view.Menu;
import android`enter code here`.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button Yes,No, Cancel;
Object myObject = new Object();
RequiresPermission.Read FunctionCalling;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Yes = (Button)findViewById(R.id.Yes);
No = (Button)findViewById(R.id.No);
Cancel= (Button)findViewById(R.id.Cancel);
Yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm Delete...");
builder.setMessage("Are you sure you want delete this?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
No.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm No)...");
builder.setMessage("Are you sure you want delete this?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
Cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm Cancel...");
builder.setMessage("Are you sure you want delete this?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on Cancel", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
}
}