簡體   English   中英

單擊按鈕時,不會出現“警報”對話框

[英]When button is clicked the Alert Dialog does not appear

我有一個警報對話框,其中有一個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();

    }

 });

}}

請幫忙。

您的新onClickListener應該是新的View.onClickListener。 您應該從以下內容開始:

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

 @Override
 public void onClick(View v){
     AlertDialog.Builder builder = new Builder(this);
        builder.setTitle("Are you sure you want to exit?")
        .setIcon(R.drawable.ic_launcher)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
                finish();
            }
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        }).create().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>

然后,您可以將您的偵聽器添加到其中。

希望這可以幫助..:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM