如何创建自定义警报对话框

时间:2013-04-27 04:42:44

标签: android layout android-alertdialog

这是google所说的用于创建自定义提醒对话框的代码(它表示创建自己的布局,然后将该布局用于setContentView)

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();

// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_signin, null))
// Add action buttons
       .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int id) {
               // sign in the user ...
           }
       })
       .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               LoginDialogFragment.this.getDialog().cancel();
           }
       });      
return builder.create();
}
然后google说要在另一个类中创建这个类的实例然后用show()方法显示它,但show方法需要一个片段管理器,当你尝试创建它时片段管理器会收到错误。

这是说明http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout

我需要在启动新活动的警报对话框中添加一个微调器

如果有人能弄清楚如何让这个工作(弹出对话框)并发布它会很棒

1 个答案:

答案 0 :(得分:1)

您可以通过这种方式创建Alert Dialog with spinner

<强>更新

 public class WvActivity extends Activity {

 TextView tx;
 String[] s = { "India ", "Arica", "India ", "Arica", "India ", "Arica",
    "India ", "Arica", "India ", "Arica" };
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 final ArrayAdapter<String> adp = new ArrayAdapter<String>(WvActivity.this,
        android.R.layout.simple_spinner_item, s);

 tx= (TextView)findViewById(R.id.txt1);
 final Spinner sp = new Spinner(WvActivity.this);
 sp.setLayoutParams(new     LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
 sp.setAdapter(adp);

 AlertDialog.Builder builder = new AlertDialog.Builder(WvActivity.this);
 builder.setView(sp);
 builder.create().show();
 }
}