AndroidDialog對話框?qū)嵗a講解

Dialog的基本方法

創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比天柱網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式天柱網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋天柱地區(qū)。費用合理售后完善,十多年實體公司更值得信賴。

//創(chuàng)建Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//設(shè)置標(biāo)題圖標(biāo)
builder.setIcon(R.drawable.ic_launcher);
//設(shè)置標(biāo)題
builder.setTitle("這是一個對話框");
//設(shè)置信息
builder.setMessage("是否要跳轉(zhuǎn)?");
//確定按鈕
setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener)
//取消按鈕
setNegativeButton (CharSequence text, DialogInterface.OnClickListener listener)
//忽略
setNeutralButton (CharSequence text, DialogInterface.OnClickListener listener)
//顯示對話框
show();

系統(tǒng)樣式

1.下拉列表

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
     builder.setIcon(R.drawable.ic_launcher);
     builder.setTitle("選擇一個城市");
     //下拉列表的數(shù)據(jù)源
     final String[] cities = {"北京", "上海", "廣州", "深圳", "杭州"};
     builder.setItems(cities, new DialogInterface.OnClickListener(){
       @Override
       public void onClick(DialogInterface dialog, int which){
         Toast.makeText(MainActivity.this, "你選擇的城市為:" + cities[which], Toast.LENGTH_SHORT).show();
       }
 });
 builder.show();

2.單選框列表

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setIcon(R.drawable.ic_launcher);
    builder.setTitle("請選擇性別");
    final String[] sex = {"男", "女"};
    //第二個參數(shù)指定默認(rèn)哪個單選框被勾中
    builder.setSingleChoiceItems(sex, 1, new DialogInterface.OnClickListener(){
      @Override
      public void onClick(DialogInterface dialog, int which){
          Toast.makeText(MainActivity.this, "性別為:" + sex[which], Toast.LENGTH_SHORT).show();
      }
    });
    builder.setPositiveButton("確定", new DialogInterface.OnClickListener(){
      @Override
      public void onClick(DialogInterface dialog, int which){
            
      }
    });
    builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
      @Override
      public void onClick(DialogInterface dialog, int which){
            
      }
    });
builder.show();

3.多選框列表

String str;
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("選擇你想看的電視劇");
final String[] hobbies = {"廷禧攻略", "扶搖", "香蜜沉沉燼如霜", "如懿傳"};
//第二個參數(shù)代表哪幾個選項被選擇,需要傳遞一個boolean[]數(shù)組進(jìn)去,其長度要和第一個參數(shù)的長度相同,如果null表示都不選
builder.setMultiChoiceItems(hobbies, null, new DialogInterface.OnMultiChoiceClickListener(){
  @Override
  public void onClick(DialogInterface dialog, int which, boolean isChecked){
    if(isChecked){
      str.append(hobbies[which] + ", ");
    }
    Toast.makeText(MainActivity.this, "選擇的是:" + str.toString(), Toast.LENGTH_SHORT).show();
  }
 });
builder.setPositiveButton("確定", new DialogInterface.OnClickListener(){
  @Override
  public void onClick(DialogInterface dialog, int which){

  }
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
  @Override
  public void onClick(DialogInterface dialog, int which){

  }
});
builder.show();

 4.等待對話框

ProgressDialog waitingDialog= new ProgressDialog(MainActivity.this);
waitingDialog.setTitle("等待加載中,請稍后...");
waitingDialog.setMessage("等待中...");
waitingDialog.setIndeterminate(true);//采用不確定式的進(jìn)度條
waitingDialog.setCancelable(false);//點擊外部不取消對話框
waitingDialog.show();

5.進(jìn)度條對話框

int MAXPD = 100;
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setProgress(0);//設(shè)置默認(rèn)值
progressDialog.setTitle("正在下載");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//進(jìn)度條樣式
progressDialog.setMax(MAXPD);//設(shè)置最大值
progressDialog.show();

自定義Dialog

1.繼承Dialog

public class CustomDialog extends Dialog {
//標(biāo)題
protected TextView hintTv;
//左邊按鈕
protected Button doubleLeftBtn;
//右邊按鈕
protected Button doubleRightBtn;
//輸入框
public EditText editText;
public CustomDialog(Context context) {
  super(context, R.style.CustomDialogStyle);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  this.setCancelable(false); // 是否可以撤銷
  setContentView(R.layout.dialog_custom);
  hintTv = findViewById(R.id.tv_common_dialog_hint);
  doubleLeftBtn = findViewById(R.id.btn_common_dialog_double_left);
  doubleRightBtn = findViewById(R.id.btn_common_dialog_double_right);
  editText = findViewById(R.id.et_common_dialog);
}
//置右鍵文字和點擊事件
public void setRightButton(String rightStr, View.OnClickListener clickListener) {
  doubleRightBtn.setOnClickListener(clickListener);
  doubleRightBtn.setText(rightStr);
}
//設(shè)置左鍵文字和點擊事件
public void setLeftButton(String leftStr, View.OnClickListener clickListener) {
  doubleLeftBtn.setOnClickListener(clickListener);
  doubleLeftBtn.setText(leftStr);
}
//設(shè)置提示內(nèi)容
public void setHintText(String str) {
  hintTv.setText(str);
  hintTv.setVisibility(View.VISIBLE);
}
//給兩個按鈕 設(shè)置文字
public void setBtnText(String leftStr, String rightStr) {
  doubleLeftBtn.setText(leftStr);
  doubleRightBtn.setText(rightStr);
}
}

2.自定義Style

<style name="CustomDialogStyle" parent="@android:style/Theme.Dialog">
    <!-- 邊框 -->
    <item name="android:windowFrame">@null</item>
    <!-- 背景透明 -->
    <item name="android:windowBackground">@color/transparent</item>
    <!-- 無標(biāo)題 -->
    <item name="android:windowNoTitle">true</item>
    <!-- 是否浮現(xiàn)在activity之上 -->
    <item name="android:windowIsFloating">true</item>
    <!-- 半透明 -->
    <item name="android:windowIsTranslucent">false</item>
    <!-- 背景模糊 -->
    <item name="android:windowContentOverlay">@null</item>
    <!-- 允許對話框的背景變暗 -->
    <item name="android:backgroundDimEnabled">true</item>
    <!-- 字體顏色 -->
    <item name="android:textColor">@color/white</item>
    <item name="android:editTextColor">@color/white</item>
 </style>

3.自定義布局

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/ll_common_dialog_layout"
  android:layout_width="500dp"
  android:layout_height="250dp"
  android:layout_margin="5dp"
  android:background="@drawable/background_info"
  android:orientation="vertical"
  android:gravity="center">

  <!--提示信息-->
  <TextView
    android:id="@+id/tv_common_dialog_hint"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textColor="@color/white"
    android:textSize="27sp"/>
  <EditText
    android:id="@+id/et_common_dialog"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:textColor="@color/back"
    android:inputType="text|textNoSuggestions"
    tools:ignore="LabelFor"
    android:hint="請輸入密碼"/>
  
  <!--底部按鈕-->
  <LinearLayout
    android:id="@+id/ll_common_dialog_double"
    android:layout_width="360dp"
    android:layout_height="60dp"
    android:layout_margin="20dp"
    android:orientation="horizontal">

    <Button
      android:id="@+id/btn_common_dialog_double_left"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@drawable/btnselector"
      android:gravity="center"
      android:textColor="@color/white"
      android:textSize="27dp"/>

    <Button
      android:id="@+id/btn_common_dialog_double_right"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@drawable/btnselector"
      android:gravity="center"
      android:textColor="@color/white"
      android:textSize="27dp"/>
  </LinearLayout>
</LinearLayout>

4.ipad隱藏底部虛擬按鍵

//彈出dialog時隱藏底部虛擬按鍵
dialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
dialog.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
  @Override
  public void onSystemUiVisibilityChange(int visibility) {
    int uiOptions =View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
    if (Build.VERSION.SDK_INT >= 19) {
      uiOptions |= 0x00001000;
    } else {
      uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
    }
    dialog.getWindow().getDecorView().setSystemUiVisibility(uiOptions);
  }
});

5.使用自定義Dialog

CustomDialog dialog = new CustomDialog(this);
dialog.setHintText("請輸入密碼");
dialog.setLeftButton("取消", new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      dialog.dismiss();
    }
});
dialog.setRightButton("確定", new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        
    }
});
dialog.show();

總結(jié)

以上所述是小編給大家介紹的Android Dialog對話框?qū)嵗a講解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!

文章標(biāo)題:AndroidDialog對話框?qū)嵗a講解
文章轉(zhuǎn)載:http://muchs.cn/article8/igship.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司、網(wǎng)站營銷、企業(yè)建站、小程序開發(fā)、網(wǎng)站內(nèi)鏈、電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司