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

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條評論
主站蜘蛛池模板: 久久频这里精品99香蕉久网址 | 狠狠艹| 日韩欧美日本 | 国产系列欧美系列日韩系列在线 | www.久操| 久久久久久天天夜夜天天 | 国产成人综合亚洲动漫在线 | 欧美一级毛片欧美一级无片 | 日本特一级毛片免费视频 | 亚洲精品国产福利片 | 四虎影视国产精品亚洲精品hd | 黄色在线观看www | 欧美不卡精品中文字幕日韩 | 99精彩视频 | 日本精品一二三区 | 亚欧成人毛片一区二区三区四区 | 精品煌色视频网站在线观看 | 日韩在线1 | 国产欧美精品一区二区 | 久久精品亚洲一级毛片 | 欧美理论在线观看 | 亚洲三级视频在线 | 久草久操 | 亚洲婷婷综合色高清在线 | 精品亚洲欧美高清不卡高清 | 一区二区免费播放 | 国产在线麻豆精品 | a级毛片在线观看 | 青青青国产精品手机在线观看 | xxx大片免费视频 | 欧美一级看片a免费视频 | 全部免费的毛片在线看美国 | 成人ab片| 久久青草免费线观最新 | 久热中文字幕在线精品免费 | 国产99精品在线观看 | 国产免费一级精品视频 | 亚洲一区二区三区首页 | 欧美在线观看高清一二三区 | 国内精品自在自线视频香蕉 | 久久久99精品久久久 |