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

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亚洲精品久久久久99 | 亚洲国产日韩欧美高清片a 亚洲国产日韩欧美一区二区三区 | 国产精品亚洲一区二区三区 | 欧美日韩大片 | 国产精品久久久久免费a∨ 国产精品久久久久免费视频 | 日日摸夜夜爽人人添 | 久久99亚洲精品久久久久 | 18p爽视频在线观看免费 | 99精品这里只有精品高清视频 | 全部免费的毛片在线看美国 | 久久一区二区三区免费播放 | 国产在线一区二区三区欧美 | 一级毛片短视频 | 久久99精品久久久久久水蜜桃 | 波多野结衣一区二区三区 | 久久久久久久亚洲精品一区 | 日本高清视频不卡 | 亚洲婷婷网 | 久久国产免费观看精品1 | 国产成人久久蜜一区二区 | 久热国产视频 | 色综合夜夜嗨亚洲一二区 | 欧美性猛片xxxxⅹ免费 | 久久久久草 | 免费黄色毛片视频 | 亚洲国产美女 | 国内精品一级毛片免费看 | 色综合欧美综合天天综合 | 夜色99 | 久久精品综合视频 | 国产中日韩一区二区三区 | 久久亚洲这里只有精品18 | 四虎影院在线看 | 欧日韩一区二区三区 | 六月婷婷中文字幕 | 色视频在线看 | 一级片免 | 91麻豆精品国产91久久久久久 | 亚洲黄色a |