[英]Why does my alert dialog not appear?
Can sombody please help me? 有人可以帮我吗? When I click on the button nothing happens. 当我单击按钮时,什么也没有发生。 I'm very new to android-programming so please answer as i can understand. 我对android编程非常陌生,所以请回答,因为我能理解。
(Don't wonder about my variables) (不要怀疑我的变量)
Thank you 谢谢
@Override
public void onClick(View v) {
Button preis = (Button) findViewById(R.id.essenpreis);
preis.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Creating alert Dialog with one Button
AlertDialog.Builder alertDialog = new AlertDialog.Builder(options.this);
// Setting Dialog Title
alertDialog.setTitle("Essenspreis");
// Setting Dialog Message
alertDialog.setMessage("Neuen Preis eintragen:");
// Setting Icon to Dialog
// alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(),"Preis geändert!", Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
}
});
}
}
Your clicklistener for button is defined inside the method body of onClick interface thats why your dialog is not showing, 按钮的clicklistener是在onClick界面的方法主体内定义的,这就是对话框未显示的原因,
This is how to show an alertDialog 这是显示警告对话框的方法
Button preis = (Button) findViewById(R.id.essenpreis);
// add button listener
preis.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
Note that you are setting listener to button with id R.id.essenpreis
after first onClick
method is performed, check if you are assigning that click listener
to anyone, your code work if you extract the button
setup from the first onClick
请注意,在执行第一个onClick
方法之后,您要将侦听器设置为ID为R.id.essenpreis
按钮,请检查是否将该click listener
器分配给任何人,如果您从第一个onClick
提取button
设置,则代码会工作
Button preis = (Button) findViewById( android.R.id.button1 );
preis.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Creating alert Dialog with one Button
AlertDialog.Builder alertDialog = new AlertDialog.Builder( MainActivity.this );
// Setting Dialog Title
alertDialog.setTitle("Essenspreis");
// Setting Dialog Message
alertDialog.setMessage("Neuen Preis eintragen:");
// Setting Icon to Dialog
// alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(),"Preis geändert!", Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
}
});
I am assuming that the options.java is your main source file. 我假设options.java是您的主要源文件。
If you have created a layout in XML, then please give id to the button as: 如果您已使用XML创建布局,则请为按钮提供id为:
android:id = "@+id/button1"
If you haven't created any layout then create a file main.xml in res folder as follows 如果尚未创建任何布局,请在res文件夹中创建一个文件main.xml ,如下所示
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/buttonAlert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Alert Box" />
</LinearLayout>
Now in the activity, I suppose here it is Options.java from the code. 现在,在活动中,我想这里是代码中的Options.java。
in the onCreate method write the following code : 在onCreate方法中,编写以下代码:
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
options.this);
// set title
alertDialogBuilder.setTitle("Essenspreis");
// set dialog message
alertDialogBuilder
.setMessage("Neuen Preis eintragen:")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.