今天就跟大家聊聊有關(guān)在Android應(yīng)用中使用AlertDialog對(duì)話框可以實(shí)現(xiàn)哪些功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)公司是專業(yè)的昂仁網(wǎng)站建設(shè)公司,昂仁接單;提供成都做網(wǎng)站、成都網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行昂仁網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
常用的Dialog有確認(rèn)對(duì)話框,單選按鈕對(duì)話框,多選按鈕對(duì)話框,復(fù)選按鈕對(duì)話框另外還有自定義的對(duì)話框
AlertDialog的常用方法
setTitle:為對(duì)話框設(shè)置標(biāo)題
setMessage:為對(duì)話框設(shè)置內(nèi)容
setIcon:為對(duì)話框設(shè)置圖標(biāo)
setItems設(shè)置對(duì)話框要顯示的list
setMultiChoiceItems:一般用于復(fù)選框顯示
setSingleChoiceItem:,設(shè)置單選按鈕
setNeutralButton:普通按鈕
setPositiveButton:添加確定按鈕
setNegativeButton:添加取消按鈕
setView:設(shè)置自定義樣式
下面通過(guò)一個(gè)實(shí)例來(lái)了解這些方法
MainActivity.class
package com.example.alertdialog;
import android.R.bool;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.pm.LabeledIntent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private int mark=0;
private String item[] = { "學(xué)生", "工人", "教師", "農(nóng)民" };
private String multChoice[]={"旅游","電影","運(yùn)動(dòng)","讀書(shū)"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.btn_button1);
button2 = (Button) findViewById(R.id.btn_button2);
button3 = (Button) findViewById(R.id.btn_button3);
button4 = (Button) findViewById(R.id.btn_button4);
button5 = (Button) findViewById(R.id.btn_button5);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
//確認(rèn)對(duì)話框
case R.id.btn_button1: {
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("確認(rèn)對(duì)話框");
builder.setMessage("確認(rèn)退出?");
builder.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "你單擊了確定按鈕",
Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "你單擊了取消按鈕",
Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
break;
}
//列表對(duì)話框
case R.id.btn_button2: {
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("職業(yè)");
builder.setItems(item, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "你的職業(yè)是" + item[which],
Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
break;
}
//多選對(duì)話框
case R.id.btn_button3: {
final boolean choose[]=new boolean[multChoice.length];
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("愛(ài)好");
builder.setMultiChoiceItems(multChoice, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
choose[which]=isChecked;
}
});
builder.setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String result="";
for(int i=0;i<multChoice.length;i++){
if(choose[i]){
result+=multChoice[i]+" ";
}
}
Toast.makeText(MainActivity.this, "你的愛(ài)好["+result+"]", Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
break;
}
//單選對(duì)話框
case R.id.btn_button4: {
mark=0;
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("職業(yè)");
builder.setSingleChoiceItems(item, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
mark=which;
}
});
builder.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "你的職業(yè):"+item[mark], Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
break;
}
//自定義對(duì)話框
case R.id.btn_button5: {
LayoutInflater inflater=LayoutInflater.from(this);
View view=inflater.inflate(R.layout.item, null);
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("自定義對(duì)話框");
builder.setView(view);
builder.setNeutralButton("普通按鈕", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"我是自定義的對(duì)話框哦",Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
break;
}
}
}
}
布局文件
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <Button android:id="@+id/btn_button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="確認(rèn)對(duì)話框" /> <Button android:id="@+id/btn_button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="列表對(duì)話框" /> <Button android:id="@+id/btn_button3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="多選對(duì)話框" /> <Button android:id="@+id/btn_button4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="單選對(duì)話框" /> <Button android:id="@+id/btn_button5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="自定義對(duì)話框" /> </LinearLayout> </RelativeLayout>
自定義的對(duì)話框布局文件
item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitCenter" android:src="@drawable/icon" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="我是自定義的對(duì)話框" /> </LinearLayout>
看完上述內(nèi)容,你們對(duì)在Android應(yīng)用中使用AlertDialog對(duì)話框可以實(shí)現(xiàn)哪些功能有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
當(dāng)前文章:在Android應(yīng)用中使用AlertDialog對(duì)話框可以實(shí)現(xiàn)哪些功能
URL鏈接:http://www.chinadenli.net/article38/iehdpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、App開(kāi)發(fā)、品牌網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)