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

Android仿IOS UIAlertView對話框

 更新時間:2018年06月27日 14:16:27   作者:祝福  
這篇文章主要為大家詳細介紹了Android仿IOS UIAlertView對話框,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android仿IOS UIAlertView對話框的具體代碼,供大家參考,具體內(nèi)容如下

顯示效果:

我在參考鏈接中看到了作者的仿的qq提示框,但是在使用的時候并不是很方面,有一些不足,于是我參照Android系統(tǒng)AlertDialog,使用參考鏈接中的布局文件和style文件,用自己的方法自定義了一下這個仿IOS上面UIAlertView的效果,這樣的話讓我們可以想使用系統(tǒng)AlertDialog一樣使用我自定義的CustomDialog。

CustomDialog使用代碼:

package com.example.iosalertview; 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
 
import com.example.iosalertview.CustomDialog.Builder; 
 
public class MainActivity extends Activity implements OnClickListener{ 
 private Button ios_dialog_btn,android_dialog_btn; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
   
  ios_dialog_btn = (Button) findViewById(R.id.ios_dialog_btn); 
  android_dialog_btn = (Button) findViewById(R.id.android_dialog_btn); 
   
  ios_dialog_btn.setOnClickListener(this); 
  android_dialog_btn.setOnClickListener(this); 
   
 } 
 
 @Override 
 public void onClick(View v) { 
  switch (v.getId()) { 
  case R.id.ios_dialog_btn: 
   CustomDialog.Builder builder = new Builder(MainActivity.this); 
   builder.setTitle(R.string.prompt); 
   builder.setMessage(R.string.exit_app); 
   builder.setPositiveButton(R.string.confirm, null); 
   builder.setNegativeButton(R.string.cancel, null); 
   builder.create().show(); 
   break; 
  case R.id.android_dialog_btn: 
   AlertDialog.Builder mbuilder = new AlertDialog.Builder(MainActivity.this); 
   mbuilder.setTitle(R.string.prompt); 
   mbuilder.setMessage(R.string.exit_app); 
   mbuilder.setPositiveButton(R.string.confirm, null); 
   mbuilder.setNegativeButton(R.string.cancel, null); 
   mbuilder.create().show(); 
   break; 
 
  default: 
   break; 
  } 
 } 
 
} 

自定義CustomDialog代碼:

package com.example.iosalertview; 
 
import android.app.Dialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
 
public class CustomDialog extends Dialog { 
 
 public CustomDialog(Context context) { 
  super(context); 
 } 
 
 public CustomDialog(Context context, int theme) { 
  super(context, theme); 
 } 
 
 public static class Builder { 
  private Context context; //上下文對象 
  private String title; //對話框標題 
  private String message; //對話框內(nèi)容 
  private String confirm_btnText; //按鈕名稱“確定” 
  private String cancel_btnText; //按鈕名稱“取消” 
  private View contentView; //對話框中間加載的其他布局界面 
  /*按鈕堅挺事件*/ 
  private DialogInterface.OnClickListener confirm_btnClickListener; 
  private DialogInterface.OnClickListener cancel_btnClickListener; 
 
  public Builder(Context context) { 
   this.context = context; 
  } 
 
  /*設置對話框信息*/ 
  public Builder setMessage(String message) { 
   this.message = message; 
   return this; 
  } 
 
  /** 
   * Set the Dialog message from resource 
   * 
   * @param title 
   * @return 
   */ 
  public Builder setMessage(int message) { 
   this.message = (String) context.getText(message); 
   return this; 
  } 
 
  /** 
   * Set the Dialog title from resource 
   * 
   * @param title 
   * @return 
   */ 
  public Builder setTitle(int title) { 
   this.title = (String) context.getText(title); 
   return this; 
  } 
 
  /** 
   * Set the Dialog title from String 
   * 
   * @param title 
   * @return 
   */ 
  public Builder setTitle(String title) { 
   this.title = title; 
   return this; 
  } 
 
  /** 
   * 設置對話框界面 
   * @param v View 
   * @return 
   */ 
  public Builder setContentView(View v) { 
   this.contentView = v; 
   return this; 
  } 
 
  /** 
   * Set the positive button resource and it's listener 
   * 
   * @param confirm_btnText 
   * @return 
   */ 
  public Builder setPositiveButton(int confirm_btnText, 
    DialogInterface.OnClickListener listener) { 
   this.confirm_btnText = (String) context 
     .getText(confirm_btnText); 
   this.confirm_btnClickListener = listener; 
   return this; 
  } 
 
  /** 
   * Set the positive button and it's listener 
   * 
   * @param confirm_btnText 
   * @return 
   */ 
  public Builder setPositiveButton(String confirm_btnText, 
    DialogInterface.OnClickListener listener) { 
   this.confirm_btnText = confirm_btnText; 
   this.confirm_btnClickListener = listener; 
   return this; 
  } 
 
  /** 
   * Set the negative button resource and it's listener 
   * 
   * @param confirm_btnText 
   * @return 
   */ 
  public Builder setNegativeButton(int cancel_btnText, 
    DialogInterface.OnClickListener listener) { 
   this.cancel_btnText = (String) context 
     .getText(cancel_btnText); 
   this.cancel_btnClickListener = listener; 
   return this; 
  } 
 
  /** 
   * Set the negative button and it's listener 
   * 
   * @param confirm_btnText 
   * @return 
   */ 
  public Builder setNegativeButton(String cancel_btnText, 
    DialogInterface.OnClickListener listener) { 
   this.cancel_btnText = cancel_btnText; 
   this.cancel_btnClickListener = listener; 
   return this; 
  } 
 
  public CustomDialog create() { 
   LayoutInflater inflater = (LayoutInflater) context 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
   // instantiate the dialog with the custom Theme 
   final CustomDialog dialog = new CustomDialog(context, R.style.mystyle); 
   View layout = inflater.inflate(R.layout.customdialog, null); 
   dialog.addContentView(layout, new LayoutParams( 
     LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 
   // set the dialog title 
   ((TextView) layout.findViewById(R.id.title)).setText(title); 
   ((TextView) layout.findViewById(R.id.title)).getPaint().setFakeBoldText(true);; 
   // set the confirm button 
   if (confirm_btnText != null) { 
    ((Button) layout.findViewById(R.id.confirm_btn)) 
      .setText(confirm_btnText); 
    if (confirm_btnClickListener != null) { 
     ((Button) layout.findViewById(R.id.confirm_btn)) 
       .setOnClickListener(new View.OnClickListener() { 
        public void onClick(View v) { 
         confirm_btnClickListener.onClick(dialog, 
           DialogInterface.BUTTON_POSITIVE); 
        } 
       }); 
    } 
   } else { 
    // if no confirm button just set the visibility to GONE 
    layout.findViewById(R.id.confirm_btn).setVisibility( 
      View.GONE); 
   } 
   // set the cancel button 
   if (cancel_btnText != null) { 
    ((Button) layout.findViewById(R.id.cancel_btn)) 
      .setText(cancel_btnText); 
    if (cancel_btnClickListener != null) { 
     ((Button) layout.findViewById(R.id.cancel_btn)) 
       .setOnClickListener(new View.OnClickListener() { 
        public void onClick(View v) { 
         cancel_btnClickListener.onClick(dialog, 
           DialogInterface.BUTTON_NEGATIVE); 
        } 
       }); 
    } 
   } else { 
    // if no confirm button just set the visibility to GONE 
    layout.findViewById(R.id.cancel_btn).setVisibility( 
      View.GONE); 
   } 
   // set the content message 
   if (message != null) { 
    ((TextView) layout.findViewById(R.id.message)).setText(message); 
   } else if (contentView != null) { 
    // if no message set 
    // add the contentView to the dialog body 
    ((LinearLayout) layout.findViewById(R.id.message)) 
      .removeAllViews(); 
    ((LinearLayout) layout.findViewById(R.id.message)).addView( 
      contentView, new LayoutParams( 
        LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT)); 
   } 
   dialog.setContentView(layout); 
   return dialog; 
  } 
 
 } 
} 

demo下載地址:Android仿IOS UIAlertView對話框

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Android 實現(xiàn)微信登錄詳解

    Android 實現(xiàn)微信登錄詳解

    本文主要介紹Android 微信登錄分享朋友圈,這里給大家詳細介紹了Android微信登錄的詳細流程,有需要的小伙伴可以參考下
    2016-07-07
  • Flutter檢查連接網(wǎng)絡connectivity_plus實現(xiàn)步驟

    Flutter檢查連接網(wǎng)絡connectivity_plus實現(xiàn)步驟

    這篇文章主要為大家介紹了Flutter檢查連接網(wǎng)絡connectivity_plus實現(xiàn)步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • Android開發(fā)之Button事件實現(xiàn)與監(jiān)聽方法總結

    Android開發(fā)之Button事件實現(xiàn)與監(jiān)聽方法總結

    這篇文章主要介紹了Android開發(fā)之Button事件實現(xiàn)與監(jiān)聽方法,結合實例形式總結分析了Android開發(fā)中Button事件的兩種實現(xiàn)方法以及針對Button控件的幾種常用監(jiān)聽方法,需要的朋友可以參考下
    2016-01-01
  • 比較完整的android MP3 LRC歌詞滾動高亮顯示(附源碼)

    比較完整的android MP3 LRC歌詞滾動高亮顯示(附源碼)

    比較完整的android MP3 LRC歌詞滾動高亮顯示(附源碼)。需要的朋友可以過來參考下,希望對大家有所幫助
    2013-11-11
  • Android SurfaceView基礎用法詳解

    Android SurfaceView基礎用法詳解

    這篇文章主要介紹了Android SurfaceView基礎用法詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Flutter將整個App變?yōu)榛疑暮唵螌崿F(xiàn)方法

    Flutter將整個App變?yōu)榛疑暮唵螌崿F(xiàn)方法

    Flutter?是?Google?開源的?UI?工具包,幫助開發(fā)者通過一套代碼庫高效構建多平臺精美應用,這篇文章主要給大家介紹了關于Flutter將整個App變?yōu)榛疑膶崿F(xiàn)方法,在Flutter中實現(xiàn)整個App變?yōu)榛疑欠浅:唵蔚?需要的朋友可以參考下
    2021-12-12
  • Android 手機無法連接mac解決辦法

    Android 手機無法連接mac解決辦法

    有沒有遇到這種情況的朋友,自己的小米2或其他Android手機無法連接mac?下面教給大家一個簡單的方法解決這個問題,只需在mac的終端輸入一行命令即可,需要的朋友可以參考下
    2016-12-12
  • Android中RecyclerView點擊Item設置事件

    Android中RecyclerView點擊Item設置事件

    這篇文章主要介紹了Android中RecyclerView點擊Item設置事件的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-07-07
  • Android Studio引入FFmpeg的方法

    Android Studio引入FFmpeg的方法

    這篇文章主要介紹了Android Studio引入FFmpeg的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Android自定義View仿支付寶芝麻信用分儀表盤

    Android自定義View仿支付寶芝麻信用分儀表盤

    前幾天支付寶剛剛升級到v9.9,看了一眼里面的芝麻信用分,儀表盤挺好看的,所以想著來寫一個這個版本的儀表盤,不說完全一模一樣,只是為了猜測支付寶在做這個的時候是如何設計的,在此記錄一下,有需要的可以參考借鑒。
    2016-09-09

最新評論