欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android常用的AlertDialog對話框及自定義對話框

 更新時間:2017年04月05日 10:22:11   作者:甄情  
本文主要介紹了android常用的AlertDialog對話框及自定義對話框的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧

常用的Dialog有確認(rèn)對話框,單選按鈕對話框,多選按鈕對話框,復(fù)選按鈕對話框另外還有自定義的對話框

AlertDialog的常用方法

setTitle:為對話框設(shè)置標(biāo)題

setMessage:為對話框設(shè)置內(nèi)容

setIcon:為對話框設(shè)置圖標(biāo)

setItems設(shè)置對話框要顯示的list

setMultiChoiceItems:一般用于復(fù)選框顯示

setSingleChoiceItem:,設(shè)置單選按鈕

setNeutralButton:普通按鈕

setPositiveButton:添加確定按鈕

setNegativeButton:添加取消按鈕

setView:設(shè)置自定義樣式

下面通過一個實例來了解這些方法

這是運行結(jié)果:

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[]={"旅游","電影","運動","讀書"}; 
 @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)對話框 
 case R.id.btn_button1: { 
 AlertDialog.Builder builder = new Builder(this); 
 builder.setIcon(R.drawable.ic_launcher); 
 builder.setTitle("確認(rèn)對話框"); 
 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; 
 } 
 //列表對話框 
 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; 
 } 
 //多選對話框 
 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("愛好"); 
 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, "你的愛好["+result+"]", Toast.LENGTH_SHORT).show(); 
 } 
 }); 
 builder.create(); 
 builder.show(); 
 break; 
 } 
 //單選對話框 
 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; 
 } 
 //自定義對話框 
 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("自定義對話框"); 
 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,"我是自定義的對話框哦",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)對話框" /> 
 <Button 
 android:id="@+id/btn_button2" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:text="列表對話框" /> 
 <Button 
 android:id="@+id/btn_button3" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:text="多選對話框" /> 
 <Button 
 android:id="@+id/btn_button4" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:text="單選對話框" /> 
 <Button 
 android:id="@+id/btn_button5" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:text="自定義對話框" /> 
 </LinearLayout> 
</RelativeLayout> 

自定義的對話框布局文件

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="我是自定義的對話框" 
 /> 
</LinearLayout> 

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關(guān)文章

  • Android基于ViewFilpper實現(xiàn)文字LED顯示效果示例

    Android基于ViewFilpper實現(xiàn)文字LED顯示效果示例

    這篇文章主要介紹了Android基于ViewFilpper實現(xiàn)文字LED顯示效果,結(jié)合完整實例形式分析了Android使用ViewFilpper實現(xiàn)文字LED顯示動畫效果的相關(guān)步驟與實現(xiàn)技巧,需要的朋友可以參考下
    2017-08-08
  • Android拖拽助手ViewDragHelper的創(chuàng)建與使用實例

    Android拖拽助手ViewDragHelper的創(chuàng)建與使用實例

    ViewDragHelper是針對 ViewGroup 中的拖拽和重新定位 views 操作時提供了一系列非常有用的方法和狀態(tài)追蹤,下面這篇文章主要給大家介紹了關(guān)于Android拖拽助手ViewDragHelper的創(chuàng)建與使用的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • Android編程獲取APP應(yīng)用程序基本信息輔助類【APP名稱、包名、圖標(biāo),版本號等】

    Android編程獲取APP應(yīng)用程序基本信息輔助類【APP名稱、包名、圖標(biāo),版本號等】

    這篇文章主要介紹了Android編程獲取APP應(yīng)用程序基本信息輔助類,可實現(xiàn)針對APP名稱、包名、圖標(biāo),版本號等信息的獲取功能,需要的朋友可以參考下
    2017-12-12
  • Android Studio 3.0中mipmap-anydpi-v26是什么東東

    Android Studio 3.0中mipmap-anydpi-v26是什么東東

    在Android Studio 3.0中一旦我們創(chuàng)建了一個項目,一個名為mipmap-anydpi-v26自動創(chuàng)建的文件夾在res文件夾下。它究竟能干什么?為什么我們需要這個?我們在開發(fā)時該如何利用它,下面通過本文給大家介紹下
    2017-12-12
  • Android仿支付寶支付從底部彈窗效果

    Android仿支付寶支付從底部彈窗效果

    這篇文章主要為大家詳細(xì)介紹了Android仿支付寶選擇支付方式,實現(xiàn)支付寶付款方式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android使用MediaRecorder實現(xiàn)錄音及播放

    Android使用MediaRecorder實現(xiàn)錄音及播放

    這篇文章主要為大家詳細(xì)介紹了Android使用MediaRecorder實現(xiàn)錄音及播放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Android仿淘寶商品詳情頁效果

    Android仿淘寶商品詳情頁效果

    這篇文章主要為大家詳細(xì)介紹了Android仿淘寶商品詳情頁效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • Android重寫View并自定義屬性實例分析

    Android重寫View并自定義屬性實例分析

    這篇文章主要介紹了Android重寫View并自定義屬性的方法,結(jié)合實例形式較為詳細(xì)的分析了Android基于重寫View實現(xiàn)自定義屬性的相關(guān)布局與具體技巧,需要的朋友可以參考下
    2016-02-02
  • JetPack Compose底部導(dǎo)航欄的實現(xiàn)方法詳解

    JetPack Compose底部導(dǎo)航欄的實現(xiàn)方法詳解

    開發(fā)一個新項目,底部導(dǎo)航欄一般是首頁的標(biāo)配,在以前的xml布局中,我們可以很輕松的是用谷歌提供的BottomNavigationView或者自定義來實現(xiàn)底部導(dǎo)航的功能,在Compose中也有也提供了一個類似的控件androidx.compose.material.BottomNavigation
    2022-09-09
  • Android基礎(chǔ)之獲取LinearLayout的寬高

    Android基礎(chǔ)之獲取LinearLayout的寬高

    LinearLayout是線性布局控件,它包含的子控件將以橫向或豎向的方式排列,按照相對位置來排列所有的widgets或者其他的containers,超過邊界時,某些控件將缺失或消失。有的時候,我們需要想獲取LinearLayout寬高,下面通過這篇文章來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。
    2016-11-11

最新評論