0

起動するダイアログがあります:

// 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 コードを次に示します。

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();


    }







}
4

2 に答える 2

3

final Dialog dialog = null;グローバル変数(アクティビティレベル)を作成し、を削除しfinalて、現在ある場所で初期化します。これで問題ありません。

于 2013-06-29T02:25:10.373 に答える