-1

我不知道该代码有什么问题..模拟器可以工作,但是该按钮无法与侦听器一起使用。我做了所有我清理项目的事情,我还做了一个侦听器,它应该可以工作。

package com.example.dialog;

import android.os.Bundle;
import android.view.View;

import android.widget.Toast;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;

public class MainActivity extends Activity {
    CharSequence[] items = { "Goolge", "Apple", "MaC Os" };
    boolean[] checkedItems = new boolean[items.length];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Button btn= (Button)findViewById(R.id.dialog);
        // btn.setOnClickListener((OnClickListener) this);
    }

    public void onClick(View v) {
        showDialog(0);

    }

    protected Dialog onCreatDialog(int id) {
        switch (id) {
        case 0 :
            Builder builder = new AlertDialog.Builder(this);
            builder.setIcon(R.drawable.ic_launcher);
            builder.setTitle("this is a List of items..");
            builder.setNegativeButton("Ok",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            Toast.makeText(getBaseContext(), "Ok clicked",
                                    Toast.LENGTH_SHORT).show();

                        }
                    });
            builder.setPositiveButton("Cancel",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(getBaseContext(), "cancel cliked",
                                    Toast.LENGTH_SHORT).show();

                        }
                    });
            builder.setMultiChoiceItems(items, checkedItems,
                    new DialogInterface.OnMultiChoiceClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which,
                                boolean isChecked) {
                            Toast.makeText(
                                    getBaseContext(),
                                    items[which]
                                            + (isChecked ? " checked"
                                                    : " unchecked"),
                                    Toast.LENGTH_SHORT).show();

                        }
                    });
            return builder.create();

        }

        return null;

    }

}


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >



        <Button
            android:id="@+id/dialog"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="@string/click_me" />



</RelativeLayout>
4

1 回答 1

0

取消注释此代码:

Button btn = (Button)findViewById(R.id.dialog);
btn.setOnClickListener(new View.onClickListener(){
     @Override
     public void onClick(View v) {
     //Do your stuff
     }
});

如果您想将您的 Activity 用作 onClickListener,请在 setOnClickListener 上保留“this”,但您的 Activity 必须实现 OnClickListener 接口,并实现其方法才能工作。

于 2013-01-11T19:30:27.510 回答