2013-05-16 190 views
0

我正在嘗試查看單選按鈕的對話框,但是當我單擊按鈕時,警告對話框從不出現。以下是代碼。請讓我知道是否有任何替代解決方案。這個樣本來自API演示。當點擊按鈕時顯示AlertDialog

public class MainPage extends Activity{ 
Button start; 
private static final int DIALOG_SINGLE_CHOICE = 1; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.mainpage); 
    start = (Button) findViewById(R.id.start); 
    start.setBackgroundResource(R.drawable.read); 
    start.setOnClickListener(new View.OnClickListener() { 
     @SuppressWarnings("deprecation") 
     public void onClick(View v) { 

      showDialog(DIALOG_SINGLE_CHOICE); 

     } 
    }); 

} 

@Override 
@Deprecated 
protected Dialog onCreateDialog(int id) { 
    id = DIALOG_SINGLE_CHOICE; 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("Choose your option"); 
    builder.setSingleChoiceItems(R.array.Baani, 0, new OnClickListener() { 

     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 

     } 
    }) 
    .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 

        /* User clicked Yes so do some stuff */ 
       } 
      }) 
      .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 

        /* User clicked No so do some stuff */ 
       } 
      }) 
      .create(); 
    return super.onCreateDialog(id); 
     } 
    } 
} 

回答

2

創建此功能,並使用showAskDialog()函數在點擊事件

private void showAskDialog() { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setTitle("DialogTitle").setMessage("DialogMessage").setCancelable(true).setSingleChoiceItems(R.array.Baani, 0, new OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 

       } 
       }).setNegativeButton("No", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 

        dialog.cancel(); 
       } 
      }).setPositiveButton("YES", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 

        dialog.cancel(); 
       } 
      }); 
    // before @TronicZomB advice 
    //AlertDialog alert = builder.create(); 
    //alert.show(); 
    // after @TronicZomB advice 
    builder.show(); 
} 
+1

'.create()'是不必要的。在Builder上調用'.show()'將會創建它並將其顯示在一行代碼中。使用'.create()'的唯一原因是你可以在其他地方多次調用'.show()',但在這種情況下它不是必需的。 – TronicZomB

1

您需要return builder.show();更換return super.onCreateDialog(id);.create()將從構建器創建一個警告對話框,但它不顯示該對話框。 .show()將從構建器創建對話框並將其顯示在屏幕上。

我也認爲int id可能是不必要的。你可以嘗試下面的代碼,它應該爲你工作:

public class MainPage extends Activity{ 
Button start; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 
setContentView(R.layout.mainpage); 
start = (Button) findViewById(R.id.start); 
start.setBackgroundResource(R.drawable.read); 
start.setOnClickListener(new View.OnClickListener() { 
    @SuppressWarnings("deprecation") 
    public void onClick(View v) { 

     showDialog(DIALOG_SINGLE_CHOICE); 

    } 
}); 

} 

@Override 
public Dialog onCreateDialog() { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("Choose your option"); 
    builder.setSingleChoiceItems(R.array.Baani, 0, new OnClickListener() { 

     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 

     } 
    }) 
    .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 

        /* User clicked Yes so do some stuff */ 
       } 
      }) 
      .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 

        /* User clicked No so do some stuff */ 
       } 
      }); 
     return builder.show(); 
     } 
    } 
} 
0

你就錯了裏面onCreateDialog()

首先,您不應該這樣做id = DIALOG_SINGLE_COICE,但如果它等於DIALOG_SINGLE_CHOICE則測試id值。 *如果是,則使用AlertDialog.Builder創建Dialog並返回builder.create()。 *如果不是,請返回super.onCreateDialog(id)

但是,此方法已被棄用,您應該使用DialogFragment來代替。

+0

'.create()'不會顯示對話框。你需要返回'builder.show()'在你的「*如果是...」 – TronicZomB

0
public class MainActivity extends Activity implements OnClickListener{ 
private Button button; 
private static final int DIALOG_SINGLE_CHOICE = 1; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    button = (Button) findViewById(R.id.button); 
    button.setOnClickListener(this); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.button: 
     showDialog(DIALOG_SINGLE_CHOICE); 
     break; 

    default: 
     break; 
    } 
} 

@Override 
@Deprecated 
protected Dialog onCreateDialog(int id) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("Choose your option"); 
    builder.setSingleChoiceItems(R.array.Baani, 0, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
     } 

    }).setPositiveButton("OK", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 

      /* User clicked Yes so do some stuff */ 
     } 
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 

      /* User clicked No so do some stuff */ 
     } 
    }).create().show(); 
    return super.onCreateDialog(id); 
} 
}