android AlertDialog对话框
2012-03-10 21:41
375 查看
今天来讲讲这个类吧!
说到这个类首先要说说一些跟他有关联的类先!
首相说说AlertDialog.Builder一看这个名字,很快就能联想到这个类是可以用来创建一个AlertDialog对象的。那么我们首先就从这个AlertDialog.Builder这个类入手吧!
看看官网:
public static class
Object
它有两个构造方法:
这两个构造函数都是返回AlertDialog这个类的实例。所以我们在创建AlertDialog类时就可以使用这个方法。当然在这个类下有许多方法,比如
添加标题的方法;
添加视图的方法;
还有一个比较重要的方法:
publicAlertDialog.Builder
Since:
API Level 1
Set a listener to be invoked when the positive button of the dialog is pressed.
Parameters
Returns
This Builder object to allow for chaining of calls to set methods
添加确定的按钮,这个按钮可以带两个参数,第一个是要显示的内容的ID,第二个是一个监听器。好了,说到了监听器,我们不妨来看看这个DialogInterface.OnClickListener这个监听器。查看官方文档。
public static interface
Known
Indirect Subclasses
DatePickerDialog,DialogPreference,
EditTextPreference,
ListPreference,
MultiSelectListPreference,
Spinner,
TimePickerDialog
这是一个接口,它的间接子类都可以用这个监听器来监听事件的发生。那么我们要做的事情就是重写这个监听器底下的方法:
这个方法有两个参数,先说后面那一个,是哪一个按钮被按下了。前面那个参数可就有意思了。
它是一个DialogInterface类,我们可以看看这是一个什么东西,请看官网:
public interface
Known
Indirect Subclasses
AlertDialog,CharacterPickerDialog,
DatePickerDialog,
Dialog,
MockDialogInterface,
ProgressDialog,
TimePickerDialog
它是一个接口,底下有这么多的子类,学过面向对象的人都可以很快的反应过来这样设计的目的是为什么.对嘛!就是多态的思想了啊。不懂的请补充多态这方面的知识,可以看看马士兵老师的视频!
好了,知识就那么多,想进一步学习的请看官方文档!下面来看具体的一个例子吧!
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/username"
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" />
<EditText
android:id="@+id/username"
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" />
<TextView
android:id="@+id/password"
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" />
<EditText
android:id="@+id/password"
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" />
</LinearLayout>
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();
}
}
一气呵成的代码,中间都木有停顿的!休息下!
说到这个类首先要说说一些跟他有关联的类先!
首相说说AlertDialog.Builder一看这个名字,很快就能联想到这个类是可以用来创建一个AlertDialog对象的。那么我们首先就从这个AlertDialog.Builder这个类入手吧!
看看官网:
public static class
AlertDialog.Builder
extendsObject
java.lang.Object | |
↳ | android.app.AlertDialog.Builder |
Public Constructors | |
---|---|
AlertDialog.Builder(Context context) Constructor using a context for this builder and the AlertDialogit creates. | |
AlertDialog.Builder(Context context, int theme) Constructor using a context and theme for this builder and the AlertDialogit creates. |
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. |
还有一个比较重要的方法:
publicAlertDialog.Builder
setPositiveButton (int textId,
DialogInterface.OnClickListener listener)
Since:API Level 1
Set a listener to be invoked when the positive button of the dialog is pressed.
Parameters
textId | The resource id of the text to display in the positive button |
---|---|
listener | The DialogInterface.OnClickListenerto use. |
This Builder object to allow for chaining of calls to set methods
添加确定的按钮,这个按钮可以带两个参数,第一个是要显示的内容的ID,第二个是一个监听器。好了,说到了监听器,我们不妨来看看这个DialogInterface.OnClickListener这个监听器。查看官方文档。
public static interface
DialogInterface.OnClickListener
android.content.DialogInterface.OnClickListener |
Known
Indirect Subclasses
DatePickerDialog,DialogPreference,
EditTextPreference,
ListPreference,
MultiSelectListPreference,
Spinner,
TimePickerDialog
这是一个接口,它的间接子类都可以用这个监听器来监听事件的发生。那么我们要做的事情就是重写这个监听器底下的方法:
public abstract voidonClickSince: |
---|
dialog | The dialog that received the click. |
---|---|
which | The button that was clicked (e.g. BUTTON1) or the position of the item clicked. |
它是一个DialogInterface类,我们可以看看这是一个什么东西,请看官网:
public interface
DialogInterface
android.content.DialogInterface |
Known
Indirect Subclasses
AlertDialog,CharacterPickerDialog,
DatePickerDialog,
Dialog,
MockDialogInterface,
ProgressDialog,
TimePickerDialog
它是一个接口,底下有这么多的子类,学过面向对象的人都可以很快的反应过来这样设计的目的是为什么.对嘛!就是多态的思想了啊。不懂的请补充多态这方面的知识,可以看看马士兵老师的视频!
好了,知识就那么多,想进一步学习的请看官方文档!下面来看具体的一个例子吧!
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/username"
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" />
<EditText
android:id="@+id/username"
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" />
<TextView
android:id="@+id/password"
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" />
<EditText
android:id="@+id/password"
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" />
</LinearLayout>
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();
}
}
一气呵成的代码,中间都木有停顿的!休息下!
相关文章推荐
- android 带EditView(编辑框)的AlertDialog(对话框)及获取输入内容
- Android--自定义对话框--AlertDialog
- android&nbsp;弹出对话框之&nbsp;AlertDialog
- ANDROID 系统提示对话框(ALERTDIALOG)的使用
- Android UI系列--对话框(一)(AlertDialog,TimePickerDialog,DatePickerDialog,ProgressDialog)
- Android中AlertDialog对话框的使用
- android 系统提示对话框(AlertDialog)的使用
- 【Android】Android中AlertDialog对话框的使用实例
- android自定义AlertDialog对话框
- Android中让AlertDialog点击了按钮后对话框不消失
- Android中AlertDialog各种对话框的用法实例详解
- Android:在AlertDialog点击按钮隐藏对话框
- Android开发——AlertDialog自定义对话框
- Androidの自定义对话框AlertDialog(一)
- android AlertDialog对话框用法
- android 自定义Alertdialog对话框用于耗时加载
- 【Android】Android中AlertDialog对话框的使用实例
- Android中的几种对话框(AlertDialog)
- android 对话框AlertDialog
- Android在 Alertdialog对话框中点击消失?