最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解

 更新時(shí)間:2016年04月28日 10:04:12   作者:hester_hester  
這篇文章主要介紹了Android中AlertDialog各種對(duì)話框的用法在項(xiàng)目開發(fā)中經(jīng)常用的到,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值

 目標(biāo)效果:

程序運(yùn)行,顯示圖一的幾個(gè)按鈕,點(diǎn)擊按鈕分別顯示圖二到圖六的對(duì)話框,點(diǎn)擊對(duì)話框的某一項(xiàng)或者按鈕,也會(huì)顯示相應(yīng)的吐司輸出。

1.activity_main.xml頁面存放五個(gè)按鈕。

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" 
tools:context=".MainActivity" > 
<Button 
android:id="@+id/btnSure" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_marginTop="10dp" 
android:text="確認(rèn)對(duì)話框"/> 
<Button 
android:id="@+id/btnRadio" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_marginTop="60dp" 
android:text="單選對(duì)話框"/> 
<Button 
android:id="@+id/btnCheck" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_marginTop="110dp" 
android:text="多選對(duì)話框"/> 
<Button 
android:id="@+id/btnList" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_marginTop="160dp" 
android:text="列表對(duì)話框"/> 
<Button 
android:id="@+id/btnMy" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_marginTop="210dp" 
android:text="自定義對(duì)話框"/> 
</RelativeLayout> 

2.新建dialog.xml頁面,作為最后一個(gè)自定義對(duì)話框的布局頁面。

dialog.xml頁面:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 
<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" > 
<EditText 
android:id="@+id/edInput" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_weight="2" > 
<requestFocus /> 
</EditText> 
<Button 
android:id="@+id/btnOk" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_weight="1" 
android:text="確定" /> 
</LinearLayout> 
<ImageView 
android:id="@+id/ivPicture" 
android:layout_width="wrap_content" 
android:layout_height="280dp" 
android:src="@drawable/white" /> 
<TextView 
android:id="@+id/textView1" 
android:layout_gravity="center" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="TextView" /> 
</LinearLayout> 

3.MainActivity.java頁面處理對(duì)話框的彈出及點(diǎn)擊事件。

MainActivity.java頁面:

package com.example.alertdialog; 
import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.view.LayoutInflater; 
import android.view.Menu; 
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 btnSure,btnRadio,btnCheck,btnList,btnMy; 
private String[] sexList={"男","女"};//單選列表 
private String[] likeList={"籃球","足球","打游戲","聽音樂","看電影"};//多選列表 
private String[] itemList={"項(xiàng)目經(jīng)理","策劃","測(cè)試","美工","程序員"};//列表 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
getId();//獲取控件id 
click();//按鈕綁定點(diǎn)擊事件 
} 
/*獲取控件id*/ 
private void getId() { 
btnSure = (Button) findViewById(R.id.btnSure); 
btnRadio=(Button) findViewById(R.id.btnRadio); 
btnCheck=(Button) findViewById(R.id.btnCheck); 
btnList=(Button) findViewById(R.id.btnList); 
btnMy=(Button) findViewById(R.id.btnMy); 
} 
/*按鈕綁定點(diǎn)擊事件*/ 
private void click() { 
btnSure.setOnClickListener(this); 
btnRadio.setOnClickListener(this); 
btnCheck.setOnClickListener(this); 
btnList.setOnClickListener(this); 
btnMy.setOnClickListener(this); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
getMenuInflater().inflate(R.menu.main, menu); 
return true; 
} 
@Override 
public void onClick(View view) { 
switch (view.getId()) { 
case R.id.btnSure: 
showDialog1();//確認(rèn)對(duì)話框 
break; 
case R.id.btnRadio: 
showDialog2();//單選對(duì)話框 
break; 
case R.id.btnCheck: 
showDialog3();//多選對(duì)話框 
break; 
case R.id.btnList: 
showDialog4(); 
break; 
case R.id.btnMy: 
showDialog5(); 
break; 
} 
} 
/*確認(rèn)對(duì)話框*/ 
private void showDialog1() { 
AlertDialog.Builder builder=new AlertDialog.Builder(this); 
builder.setTitle("確認(rèn)對(duì)話框");//設(shè)置標(biāo)題 
builder.setIcon(R.drawable.ic_launcher);//設(shè)置圖標(biāo) 
builder.setMessage("確認(rèn)對(duì)話框提示內(nèi)容");//設(shè)置內(nèi)容 
/*添加對(duì)話框中確定按鈕和點(diǎn)擊事件*/ 
builder.setPositiveButton("確定",new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface arg0, int arg1) { 
Toast.makeText(MainActivity.this,"點(diǎn)擊了確定按鈕",Toast.LENGTH_SHORT).show(); 
} 
}); 
/*添加對(duì)話框中取消按鈕和點(diǎn)擊事件*/ 
builder.setNegativeButton("取消",new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface arg0, int arg1) { 
Toast.makeText(MainActivity.this,"點(diǎn)擊了取消按鈕",Toast.LENGTH_SHORT).show(); 
} 
}); 
AlertDialog dialog=builder.create();//獲取dialog 
dialog.show();//顯示對(duì)話框 
} 
/*單選對(duì)話框*/ 
private void showDialog2() { 
AlertDialog.Builder builder=new AlertDialog.Builder(this); 
builder.setTitle("性別");//設(shè)置標(biāo)題 
builder.setIcon(R.drawable.ic_launcher);//設(shè)置圖標(biāo) 
/*參數(shù)一位單選列表文字,參數(shù)二為默認(rèn)第幾個(gè)選中(-1默認(rèn)不選中),參數(shù)三是創(chuàng)建監(jiān)聽器*/ 
builder.setSingleChoiceItems(sexList,-1,new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
String sex=sexList[which]; 
Toast.makeText(MainActivity.this,"這個(gè)人性別為"+sex, Toast.LENGTH_SHORT).show(); 
} 
}); 
/*添加對(duì)話框中取消按鈕點(diǎn)擊事件*/ 
builder.setNegativeButton("取消",new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
dialog.dismiss();//關(guān)閉對(duì)話框 
} 
}); 
AlertDialog dialog=builder.create();//獲取dialog 
dialog.show();//顯示對(duì)話框 
} 
/*多選對(duì)話框*/ 
private void showDialog3() { 
AlertDialog.Builder builder=new AlertDialog.Builder(this); 
builder.setTitle("愛好");//設(shè)置標(biāo)題 
builder.setIcon(R.drawable.ic_launcher);//設(shè)置圖標(biāo) 
/*參數(shù)同單選對(duì)話框一樣,另外第二個(gè)參數(shù)默認(rèn)不選中為null,而不是-1*/ 
builder.setMultiChoiceItems(likeList,null,new DialogInterface.OnMultiChoiceClickListener() { 
@Override 
public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
if(isChecked){ 
Toast.makeText(MainActivity.this,"我喜歡"+likeList[which],Toast.LENGTH_SHORT).show(); 
}else{ 
Toast.makeText(MainActivity.this,"我不喜歡"+likeList[which],Toast.LENGTH_SHORT).show(); 
} 
} 
}); 
/*添加對(duì)話框中取消按鈕點(diǎn)擊事件*/ 
builder.setNegativeButton("取消",new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
dialog.dismiss();//關(guān)閉對(duì)話框 
} 
}); 
AlertDialog dialog=builder.create();//獲取dialog 
dialog.show();//顯示對(duì)話框 
} 
/*列表對(duì)話框*/ 
private void showDialog4() { 
AlertDialog.Builder builder=new AlertDialog.Builder(this); 
builder.setTitle("部門列表");//設(shè)置標(biāo)題 
builder.setIcon(R.drawable.ic_launcher);//設(shè)置圖標(biāo) 
builder.setItems(itemList,new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
Toast.makeText(MainActivity.this,"我點(diǎn)擊了"+itemList[which],Toast.LENGTH_SHORT).show(); 
} 
}); 
AlertDialog dialog=builder.create();//獲取dialog 
dialog.show();//顯示對(duì)話框 
} 
/*自定義對(duì)話框*/ 
private void showDialog5() { 
LayoutInflater inflater=LayoutInflater.from(this); 
View view=inflater.inflate(R.layout.dialog,null);//獲取自定義布局 
AlertDialog.Builder builder=new AlertDialog.Builder(this); 
builder.setTitle("自定義對(duì)話框");//設(shè)置標(biāo)題 
builder.setIcon(R.drawable.ic_launcher);//設(shè)置圖標(biāo) 
builder.setView(view);//設(shè)置自定義樣式布局到對(duì)話框 
AlertDialog dialog=builder.create();//獲取dialog 
dialog.show();//顯示對(duì)話框 
} 
}

4.運(yùn)行就出現(xiàn)目標(biāo)效果了。

關(guān)于Android中AlertDialog各種對(duì)話框的用法就給大家介紹這么多,希望對(duì)大家有所幫助!

相關(guān)文章

  • Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解

    Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解

    這篇文章主要介紹了Android中AlertDialog各種對(duì)話框的用法在項(xiàng)目開發(fā)中經(jīng)常用的到,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值
    2016-04-04
  • Android Glide常見使用方式講解

    Android Glide常見使用方式講解

    對(duì)于Glide這個(gè)加載圖片的框架,很多人都在用,我之前使用的是ImageLoader,最近查資料時(shí),發(fā)現(xiàn)Glide才是Google推薦的加載圖片框架,功能非常強(qiáng)大,而且還有Google專人維護(hù),要知道,ImageLoader已經(jīng)沒人維護(hù)了,除了問題可沒人解答。所以有必要整理一下Glide的使用
    2023-01-01
  • Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的詳細(xì)代碼

    Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的詳細(xì)代碼

    這篇文章主要介紹了Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的詳細(xì)代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • Android實(shí)現(xiàn)自由拖動(dòng)并顯示文字的懸浮框

    Android實(shí)現(xiàn)自由拖動(dòng)并顯示文字的懸浮框

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)自由拖動(dòng)并顯示文字的懸浮框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • Android中使用ListView繪制自定義表格技巧分享

    Android中使用ListView繪制自定義表格技巧分享

    使用ListView繪制自定義的表格有朋友嘗試過沒有,下面為大家分享下要實(shí)現(xiàn)下圖的效果有幾個(gè)方面,參照著這幾點(diǎn)做了個(gè)簡(jiǎn)單的實(shí)現(xiàn)不是問題好了,話不多說看代碼
    2013-06-06
  • Android EditText監(jiān)聽回車鍵并處理兩次回調(diào)問題

    Android EditText監(jiān)聽回車鍵并處理兩次回調(diào)問題

    這篇文章主要介紹了Android EditText監(jiān)聽回車鍵并處理兩次回調(diào)問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-08-08
  • Android使用系統(tǒng)相機(jī)進(jìn)行拍照的步驟

    Android使用系統(tǒng)相機(jī)進(jìn)行拍照的步驟

    這篇文章主要介紹了Android使用系統(tǒng)相機(jī)進(jìn)行拍照的步驟,幫助大家更好的進(jìn)行Android開發(fā),感興趣的朋友可以了解下
    2020-12-12
  • Android文件讀寫的幾種方式

    Android文件讀寫的幾種方式

    文件讀寫作為Android四大數(shù)據(jù)存儲(chǔ)方式之一,又分為內(nèi)部存儲(chǔ)和外部存儲(chǔ)兩種,下面這篇文章主要給大家介紹了關(guān)于Android文件讀寫的幾種方式,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-06-06
  • Flutter 包管理器和資源管理使用學(xué)習(xí)

    Flutter 包管理器和資源管理使用學(xué)習(xí)

    這篇文章主要為大家介紹了Flutter 包管理器和資源管理使用學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • Android實(shí)現(xiàn)生成二維碼并保存到相冊(cè)

    Android實(shí)現(xiàn)生成二維碼并保存到相冊(cè)

    這篇文章主要介紹了如何利用Android實(shí)現(xiàn)二維碼的生成,并且保存到本地相冊(cè)。文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編學(xué)習(xí)一下
    2022-04-04

最新評(píng)論

梧州市| 阿克苏市| 建水县| 明溪县| 开平市| 古蔺县| 德阳市| 绥滨县| 抚宁县| 白玉县| 徐州市| 清原| 确山县| 盐山县| 桂平市| 舒城县| 耿马| 桐柏县| 景谷| 铅山县| 兴海县| 商都县| 云梦县| 凤凰县| 垫江县| 玛纳斯县| 蓝田县| 石景山区| 霍城县| 正定县| 涞水县| 康保县| 板桥市| 扎兰屯市| 杭锦后旗| 明水县| 宁城县| 肇州县| 唐海县| 景谷| 洛南县|