onClickListener内的AlertDialog

问题描述 投票:13回答:3

我正在尝试从onClickListener启动AlertDialog,但出现以下错误。

The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined  

有人知道如何解决此问题吗?

        mRecordButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            new AlertDialog.Builder( this )
            .setTitle( "Cast Recording" )
            .setMessage( "Now recording your message" )
            .setPositiveButton( "Save", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Log.d( "AlertDialog", "Positive" );
                }
            })
            .setNegativeButton( "Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Log.d( "AlertDialog", "Negative" );
                }
            } )
            .show();
        }
    });
android android-alertdialog
3个回答
30
投票

更改此行

new AlertDialog.Builder( this );

to

new AlertDialog.Builder( YourActivity.this );

这是因为构造函数需要上下文类型&OnclickListner is not a Context type,所以您使用Activity的对象。

我希望它能帮助..


0
投票

new AlertDialog.Builder( this )中,this是指侦听器,而不是外部类实例。


0
投票

步骤1.创建文件activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#d7ecd6"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a TextView."
        android:textColor="#ff5ff4"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Apply Big Font Size"
        android:layout_below="@id/tv"
        />
</RelativeLayout>

步骤2.创建一个类MainActivity.java

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Get reference of widgets from XML layout
        final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        Button btn = (Button) findViewById(R.id.btn);
        final TextView tv = (TextView) findViewById(R.id.tv);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Build an AlertDialog
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                // Set a title for alert dialog
                builder.setTitle("Select your answer.");
                // Ask the final question
                builder.setMessage("Want to apply big font size?");
                // Set click listener for alert dialog buttons
                DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch(which){
                            case DialogInterface.BUTTON_POSITIVE:
                                // User clicked the Yes button
                                tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 35);
                                break;
                            case DialogInterface.BUTTON_NEGATIVE:
                                // User clicked the No button
                                break;
                        }
                    }
                };
                // Set the alert dialog yes button click listener
                builder.setPositiveButton("Yes", dialogClickListener);
                // Set the alert dialog no button click listener
                builder.setNegativeButton("No",dialogClickListener);
                AlertDialog dialog = builder.create();
                // Display the alert dialog on interface
                dialog.show();
            }
        });
  }
}

在线代码:AlertDialog onlick in android

© www.soinside.com 2019 - 2024. All rights reserved.