AlertDialog按钮不起作用,不会关闭 [英] AlertDialog button doesn't work and it doesn't dismiss
问题描述
package com.example.root.gbu;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.SystemClock;
import android.widget.ImageView;
public class DuelFrame {
private Context context;
private long startTime;
private long endTime;
private boolean timeToShot = false;
私人ImageView图像;
private AlertDialog alertDialog;
public DuelFrame(String enemy,Context context){
this.context = context;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(对抗+敌人);
image = new ImageView(context);
image.setImageResource(R.drawable.duel_start);
builder.setView(image);
builder.setNegativeButton(BANG,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int id)
{
System.out.println(******************** BANG PREMUTO ********************* ****);
if(timeToShot){
endTime = SystemClock.elapsedRealtimeNanos();
ConnectionStream.getOut()。println(TIME:+ String.valueOf - startTime));
}
else
ConnectionStream.getOut()。println(TIME:LOSE);
}
});
alertDialog = builder.create();
}
public void start(){
((Activity)context).runOnUiThread(new Runnable(){// mostro la finestra
@Override
public void run(){
alertDialog.show();
}
});
new java.util.Timer()。schedule(// timer per cambiare l'immagine
new java.util.TimerTask(){
@Override
public void run ){
((Activity)context).runOnUiThread(new Runnable(){
@Override
public void run(){
image.getLayoutParams()。height = image。 ();
image.getLayoutParams()。 b $ b timeToShot = true;
}
});
}
},3000);
}
public void cancel(){
((Activity)context).runOnUiThread(new Runnable(){
@Override
public void run (){
alertDialog.dismiss();
}
});
}
}
所以我需要修复onClickListener和cancel )函数
您需要调用
showDialog(int)
方法。
然后重写方法
onCreateDialog(int)
里面创建对话框并设置正负按钮,然后它将工作。
尽管这种方式已被弃用。你现在应该使用碎片。但是您仍然可以尝试一下!
检查此链接: https://developer.android.com/guide/topics/ui/dialogs.html
I have this AlertDialog that should be a duel between two players. It shows an image of a duel and after three seconds, thanks to a timer, it shows a "BANG" image. When the "BANG" image appears, the player must press the "BANG" button and the fester player wins. My problem is that the listener on the button doesn't work and i can't dismiss the alertDialog. This is the code
package com.example.root.gbu;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.SystemClock;
import android.widget.ImageView;
public class DuelFrame {
private Context context;
private long startTime;
private long endTime;
private boolean timeToShot=false;
private ImageView image;
private AlertDialog alertDialog;
public DuelFrame(String enemy, Context context){
this.context = context;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Duel against " + enemy);
image = new ImageView(context);
image.setImageResource(R.drawable.duel_start);
builder.setView(image);
builder.setNegativeButton("BANG",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
System.out.println("********************BANG PREMUTO*************************");
if(timeToShot) {
endTime = SystemClock.elapsedRealtimeNanos();
ConnectionStream.getOut().println("TIME:" + String.valueOf(endTime - startTime));
}
else
ConnectionStream.getOut().println("TIME:LOSE");
}
});
alertDialog = builder.create();
}
public void start(){
((Activity)context).runOnUiThread(new Runnable(){//mostro la finestra
@Override
public void run(){
alertDialog.show();
}
});
new java.util.Timer().schedule(//timer per cambiare l'immagine
new java.util.TimerTask() {
@Override
public void run() {
((Activity)context).runOnUiThread(new Runnable(){
@Override
public void run(){
image.getLayoutParams().height = image.getHeight();
image.getLayoutParams().width = image.getWidth();
image.setImageResource(R.drawable.bang);
startTime = SystemClock.elapsedRealtimeNanos();
timeToShot = true;
}
});
}
}, 3000);
}
public void cancel(){
((Activity)context).runOnUiThread(new Runnable(){
@Override
public void run(){
alertDialog.dismiss();
}
});
}
}
So i need to fix the onClickListener and the cancel() function
You need to call the
showDialog(int)
method.
Then override the method
onCreateDialog(int)
inside this create the dialog and set the positive and negative buttons and then it will work.
Though this way is deprecated. You should use fragments now. But still you can give it a try!
Check this link: https://developer.android.com/guide/topics/ui/dialogs.html
这篇关于AlertDialog按钮不起作用,不会关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!