gpt4 book ai didi

java - 单击按钮时,不会出现警报对话框

转载 作者:太空宇宙 更新时间:2023-11-04 15:09:24 25 4
gpt4 key购买 nike

我有一个警报对话框,其中有一个 EditText,您可以在其中输入文本,然后将其添加到 ListView。然后在每个项目上都有一个按钮,我想让该按钮成为一个 AlertDialog。下面的代码不起作用。

这是我的代码...

public class DeleteRenameList extends Activity {

Button button;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

AlertDialog.Builder alert=new AlertDialog.Builder(DeleteRenameList.this);
alert.setMessage("What do you want to do?");
alert.setPositiveButton("Rename", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {


}
});
alert.setNegativeButton("Delete", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

dialog.cancel();
}
});
alert.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {


}
});
AlertDialog ale=alert.create();
ale.show();

}

});

}}

请帮忙。

最佳答案

试试这个:

创建自定义对话框并呈现它:

//Make your class named CUstomDialogClass
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.widget.Button;

public class CustomDialogClass extends Dialog{

public Activity c;
public Dialog d;
public Button yes, no;

public CustomDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;

}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.customdialog);

}

}

In your MainActivity, place this code in your onCreate() method:


Button button = (Button)findViewById(R.id.yourid);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
final CustomDialogClass dialog = new CustomDialogClass(MainActivity.this);
dialog.setTitle("Your Title");
dialog.show();

final Timer time = new Timer();
time.schedule(new TimerTask() {
@Override
public void run() {
dialog.dismiss();
}
}, 5000);

}
});

最后,您可以修改自定义对话框的布局。我只是举了一个例子:

<?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:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yout Title"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Message"
android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

然后您可以将监听器添加到其中。

希望这有帮助..:)

关于java - 单击按钮时,不会出现警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21539478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com