Android的Dialog类设计的太糟糕了!


题目的噱头是大了点。
但是,这是不争的事实。
我们可以看看这么一个需求:我要在一个界面上显示一个对话框。不管是简单的确认对话框,还是可以选择是否的对话框。
Windows下面的代码:

C# 

private void button1_Click(object sender, System.EventArgs e) {
   if(textBox1.Text == "") {
      MessageBox.Show("You must enter a name.", "Name Entry Error",
         MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
   }
   else {
      // Code to act on the data entered would go here.
   }
}

参看这里: http://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox.aspx

但是,跑到Android下,折腾了半天还没折腾出点东西来。

先来看文档

android.app
public class

android.app.AlertDialog

java.lang.Object

android.app.Dialog
DialogInterface KeyEvent.Callback View.OnCreateContextMenuListener Window.Callback

android.app.AlertDialog
DialogInterface

A subclass of Dialog that can display one, two or three buttons. If you only want to display a String in this dialog box, use the setMessage() method. If you want to display a more complex view, look up the FrameLayout called "body" and add your view to it:

 FrameLayout fl = (FrameLayout) findViewById(R.id.body);
 fl.add(myView, new LayoutParams(FILL_PARENT, WRAP_CONTENT));
 


没看太懂吧。是的,我也没看懂。
为啥呢。这段描述就和怎么用AlertDialog一点关系都没有。
再等看到这一段,得。哥们这次彻底歇菜了。

Protected Constructors

AlertDialog(Context context)

AlertDialog(Context context, int theme)

AlertDialog(Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener)

也就是说,这个类人家压根都没有准备让你直接构造出来。咋整呢?

兄弟我都懒得说了,用代码来说明吧。

new AlertDialog.Builder(AlertDialogSamples.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.alert_dialog_two_buttons_title)
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked Cancel so do some stuff */
}
})
.create();


具体看这里: http://code.google.com/android/samples/ApiDemos/src/com/example/android/apis/app/AlertDialogSamples.html

posted @ 2008-12-03 19:51  survivedev  阅读(1964)  评论(4编辑  收藏  举报