今天来讲讲这个类吧!
说到这个类首先要说说一些跟他有关联的类先!
首相说说AlertDialog.Builder一看这个名字,很快就能联想到这个类是可以用来创建一个AlertDialog对象的。那么我们首先就从这个AlertDialog.Builder这个类入手吧!
看看官网:
java.lang.Object | |
↳ | android.app.AlertDialog.Builder |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
AlertDialog.Builder(Context context)
Constructor using a context for this builder and the AlertDialog it creates.
|
|||||||||||
AlertDialog.Builder(Context context, int theme)
Constructor using a context and theme for this builder and the AlertDialog it creates.
|
这两个构造函数都是返回AlertDialog这个类的实例。所以我们在创建AlertDialog类时就可以使用这个方法。当然在这个类下有许多方法,比如
setTitle(int titleId)
Set the title using the given resource id.
|
setView(View view)
Set a custom view to be the contents of the Dialog.
|
还有一个比较重要的方法:
Set a listener to be invoked when the positive button of the dialog is pressed.
textId | The resource id of the text to display in the positive button |
---|---|
listener | The DialogInterface.OnClickListener to use. |
android.content.DialogInterface.OnClickListener |
public abstract voidonClick (DialogInterface dialog, int which)
Since: API Level 1
This method will be invoked when a button in the dialog is clicked. Parameters
|
|||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
它是一个DialogInterface类,我们可以看看这是一个什么东西,请看官网:
android.content.DialogInterface |
好了,知识就那么多,想进一步学习的请看官方文档!下面来看具体的一个例子吧!
main.xml:
android:layout_height="fill_parent"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20.dip"
android:layout_marginRight="20.dip"
android:gravity="left"
android:text="帐号"
android:textAppearance="?android:attr/textAppearanceMedium" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:autoText="false"
android:capitalize="none"
android:gravity="fill_horizontal"
android:scrollHorizontally="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:gravity="left"
android:text="密码"
android:textAppearance="?android:attr/textAppearanceMedium" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:autoText="false"
android:capitalize="none"
android:gravity="fill_horizontal"
android:password="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
DialogTextActivity.java:
package zsc.edu.lian;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
public class DialogTextActivity extends Activity {
ProgressDialog m_Dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Dialog dialog = new AlertDialog.Builder(DialogTextActivity.this)
.setTitle("登录提示")
.setMessage("这里需要登录!")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
LayoutInflater factory = LayoutInflater
.from(DialogTextActivity.this);
final View DialogView = factory.inflate(R.layout.main,
null);
Dialog dlg = new AlertDialog.Builder(
DialogTextActivity.this)
.setTitle("登录框")
.setView(DialogView)
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
m_Dialog.show(
DialogTextActivity.this,
"请等待...", "正在为你登录...",
true);
new Thread() {
public void run() {
try {
sleep(3000);
} catch (Exception e) {
e.printStackTrace();
} finally {
m_Dialog.dismiss();
}
}
}.start();
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
DialogTextActivity.this
.finish();
}
}).create();
dlg.show();
}
})
.setNegativeButton("退出", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DialogTextActivity.this.finish();
}
}).create();
dialog.show();
}
}
一气呵成的代码,中间都木有停顿的!休息下!