亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Android的AlertDialog詳解

系統 2080 0

AlertDialog的構造方法全部是Protected的,所以不能直接通過new一個AlertDialog來創建出一個AlertDialog。

要創建一個AlertDialog,就要用到AlertDialog.Builder中的create()方法。

使用AlertDialog.Builder創建對話框需要了解以下幾個方法:

setTitle :為對話框設置標題
setIcon :為對話框設置圖標
setMessage:為對話框設置內容
setView : 給對話框設置自定義樣式
setItems :設置對話框要顯示的一個list,一般用于顯示幾個命令時
setMultiChoiceItems :用來設置對話框顯示一系列的復選框
setNeutralButton :普通按鈕

setPositiveButton :給對話框添加"Yes"按鈕
setNegativeButton :對話框添加"No"按鈕
create : 創建對話框
show :顯示對話框
一、簡單的AlertDialog

下面,創建一個簡單的ALertDialog并顯示它:


[java] package com.tianjf;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;

public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("對話框的標題").
setMessage("對話框的內容").
setIcon(R.drawable.ic_launcher).
create();
alertDialog.show();
}
}
package com.tianjf;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;

public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("對話框的標題").
setMessage("對話框的內容").
setIcon(R.drawable.ic_launcher).
create();
alertDialog.show();
}
}運行結果如下:

\" src= http://schemas.android.com/apk/res/android "
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >

<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/user" />

<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >

<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/passward" />

<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>

</LinearLayout>
<?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" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >

<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/user" />

<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >

<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/passward" />

<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>

</LinearLayout>
然后在Activity里面把Login畫面的布局文件添加到AlertDialog上

[java] package com.tianjf;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// 取得自定義View
LayoutInflater layoutInflater = LayoutInflater.from(this);
View myLoginView = layoutInflater.inflate(R.layout.login, null);

Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("用戶登錄").
setIcon(R.drawable.ic_launcher).
setView(myLoginView).
setPositiveButton("登錄", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// 取得自定義View
LayoutInflater layoutInflater = LayoutInflater.from(this);
View myLoginView = layoutInflater.inflate(R.layout.login, null);

Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("用戶登錄").
setIcon(R.drawable.ic_launcher).
setView(myLoginView).
setPositiveButton("登錄", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}運行結果如下:

\" src=





摘自 殤雲的專欄

Android的AlertDialog詳解


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 一级片在线观看视频 | 亚洲国产精品久久卡一 | 国产精品久久久久久久y | 婷婷久久五月天 | 欧美精品一区二区三区观 | 国产精品高清在线观看 | 国产第一页久久亚洲欧美国产 | 国产91一区二这在线播放 | 99性视频| 欧美成人一级毛片 | 狠狠色噜噜狠狠狠狠奇米777 | 农村寡妇一级毛片免费播放 | 高清视频 一区二区三区四区 | 亚洲不卡视频在线观看 | 亚洲成人在线免费视频 | 国产午夜精品久久理论片 | 久久国产精品男女热播 | 国产精品模特hd在线 | 久久免费香蕉视频 | 日韩免费视频观看 | 国产二区三区毛片 | 橘梨纱视频一区二区在线观看 | 久久不色 | 四虎影院在线免费 | 八戒久久精品一区二区三区 | 四虎在线观看网址 | 久久视频这里只有精品35 | 国产欧美精品综合一区 | 99v视频国产在线观看免费 | 青草伊伊| 伊人情人综合成人久久网小说 | 日韩城人免费 | 国产成人精品永久免费视频 | 国产一级特黄高清在线大片 | 九九亚洲综合精品自拍 | 中文字幕日本不卡一二三区 | 免费看成人频视在线视频 | 中中文字幕乱码 | 日本一本一道 | 精品新一区二区三区四区 | 五月婷婷综合在线 |