java - 对话框中的取消按钮不关闭对话框

标签 java android android-dialog

我有一个启动对话框:

// custom dialog
        final Dialog dialog = new Dialog(this);

        dialog.setContentView(R.layout.add_taste_dialog);

        dialog.setTitle("Add Taste");
        Spinner spinner = (Spinner) dialog.findViewById(R.id.spinner1);


        Spinner spinner2 = (Spinner) dialog.findViewById(R.id.spinner2);

        Button dialogButton = (Button) dialog.findViewById(R.id.cancelButton);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();

问题是,当我单击按钮时,它不会关闭对话框。

我的对话框 xml 是:

<?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/tastePickTitle"
        android:text="Select a Taste: "
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="20sp"
        android:textStyle = "bold"
        android:padding="5dip"
        >
    </TextView>

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/taste_array"

         />

    <View
        android:layout_width="fill_parent"
        android:layout_height="30dp">
    </View>

    <TextView
        android:id="@+id/ammountPickTitle"
        android:text="How much taste: "
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="20sp"
        android:textStyle = "bold"
        android:padding="5dip"
        >
    </TextView>

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/ammount_array"

         />

    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

     <Button
        android:id="@+id/cancelButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cancel"
        android:layout_weight="1"
         />   


     <Button
        android:id="@+id/addTasteButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"
        android:layout_weight="1"
        android:onClick="addTasteNow" />




     </LinearLayout>




</LinearLayout>

这里是完整的品味标签 java 代码,为上面的 fragment 提供更多上下文:

public class TasteTags extends Activity {

    BeerData e;



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tastetag_page);

        //get beer data
        Intent intent = getIntent();
        Bundle b = intent.getExtras();
        e = b.getParcelable("myBeerObject");

        TextView beerTitle = (TextView) findViewById(R.id.beerTitleTaste);

        beerTitle.setText(e.beerName + " Taste Profile");

        String url = "myURL";

        url = url + "b=" +e.beerId;

        //async task to get beer taste tag percents
        new GetTasteJSON(this).execute(url);




    }

    public void addTaste(View v){

        // custom dialog
        final Dialog dialog = new Dialog(this);

        dialog.setContentView(R.layout.add_taste_dialog);

        dialog.setTitle("Add Taste");
        Spinner spinner = (Spinner) dialog.findViewById(R.id.spinner1);


        Spinner spinner2 = (Spinner) dialog.findViewById(R.id.spinner2);

        Button dialogButton = (Button) dialog.findViewById(R.id.cancelButton);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Log.d("dialog", "cancel pressed");
                dialog.dismiss();
            }
        });

        dialog.show();


    }







}

最佳答案

make final Dialog dial = null; 一个全局变量( Activity 级别),删除 final 并在您现在拥有的位置进行初始化,这样就可以了。

关于java - 对话框中的取消按钮不关闭对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17375546/

相关文章:

java - Jsoup 不解析特定的 Div

java - bean 名称持续错误

java - 如何将图库图像作为输入流

android - auto 和 preferExternal 安装位置 Android list 之间的区别

android - 创建新对话框时出现 NullPointerException

java在循环中超出范围

多次调用 Android LiveData observe

java - Android 中的内联日期选择器

android对话框将图标设置为标题

android - 查看未附加到窗口管理器(解决方案是什么?)