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

标签 java android eclipse android-alertdialog

我有一个警报对话框,其中有一个 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/

相关文章:

java - 在 Eclipse IDE 中调试 Java 时将异常捕获为表达式

java - 无法创建记录器 Tomcat 错误

Java将新文件的数据附加到旧文件

android - 我应该将我的 freetype 字体加载器指向哪里?

eclipse - 在 pycharm 或 eclipse+pydev 的 C 线程中无法使用 python 断点

java - Spring MVC 注解

java - 如何从正在运行的服务获取数据

Java - Swing 监听表单文本字段中的 Action

android - 在 Android 上使用 LocationListener 禁用提供程序

Android 11 - native C++ 库的 System.loadLibrary 需要 60 多秒,在 Android 10 及更低版本上运行速度非常快