Android開發(fā)小知識2—彈窗

前言

彈窗是指打開網(wǎng)頁、軟件、手機(jī)APP等的時(shí)候自動(dòng)彈出的窗口,目前主要流行的彈出方式是快速進(jìn)入網(wǎng)頁游戲的快捷途徑。 在android彈窗是非常常見與實(shí)用的控件,相信很多朋友都使用過彈窗。這篇文章想和大家分享一下彈窗的相關(guān)知識。


彈窗的分類和區(qū)別

彈窗分為模態(tài)彈窗和非模態(tài)彈窗兩種,這兩者最主要的區(qū)別在于需不需要用戶對其進(jìn)行回應(yīng)。模態(tài)彈窗會(huì)打斷用戶的正常操作,要求用戶必須對其進(jìn)行回應(yīng),否則不能繼續(xù)其它操作行為,比如說:Dialog;非模態(tài)彈窗則不會(huì)影響用戶的操作,用戶可以不對其進(jìn)行回應(yīng),非模態(tài)彈窗通常都有時(shí)間限制,出現(xiàn)一段時(shí)間后就會(huì)自動(dòng)消失,比如說:Toast。

彈窗分類

根據(jù)模態(tài)彈窗和非模態(tài)彈窗的區(qū)別,當(dāng)只是需要告知用戶信息,不需要用戶操作,一般設(shè)計(jì)為非模態(tài)彈窗;需要用戶進(jìn)行一定的操作時(shí)我們使用模態(tài)彈窗。


仿IOS 的Dialog

IOS和Android在彈窗方面有一些不同,但是在實(shí)際開發(fā)中,設(shè)計(jì)者往往偏向于IOS的彈窗設(shè)計(jì)。所以,對于Android開發(fā)者,勢必要學(xué)習(xí)幾種常見的仿IOS彈窗。這里給大家介紹一種的仿IOS的Dialog。

布局文件,在這了可以修改dialog的式樣。

<?xml version="1.0" encoding="utf-8"?>
<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">

    <ImageView
        android:id="@+id/background_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/transparent_half"
        android:scaleType="fitXY" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="@dimen/margin40"
        android:layout_marginRight="@dimen/margin40"
        android:background="@drawable/shape_frame_gray_background_white">

        <TextView
            android:id="@+id/dialog_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginEnd="@dimen/margin10"
            android:layout_marginStart="@dimen/margin10"
            android:layout_marginTop="@dimen/margin20"
            android:gravity="center"
            android:textColor="@color/dark_gray"
            android:textSize="@dimen/text_size17"
            tools:text="Logout" />

        <TextView
            android:id="@+id/dialog_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/dialog_title"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="@dimen/margin30"
            android:layout_marginEnd="@dimen/margin10"
            android:layout_marginStart="@dimen/margin10"
            android:layout_marginTop="@dimen/margin30"
            android:gravity="center"
            android:textColor="@color/dark_gray"
            android:textSize="@dimen/text_size13"
            tools:text="Are you sure you want to log out?" />

        <View
            android:id="@+id/line_transverse"
            android:layout_width="match_parent"
            android:layout_height="@dimen/height1"
            android:layout_below="@+id/dialog_content"
            android:background="@color/gray_dialog" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/line_transverse">

            <Button
                android:id="@+id/right_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_toEndOf="@+id/line_vertical"
                android:background="@color/transparent"
                android:text="@string/dialog_yes"
                android:textAllCaps="false"
                android:textColor="@color/blue"
                android:textSize="@dimen/text_size17" />

            <View
                android:id="@+id/line_vertical"
                android:layout_width="@dimen/height1"
                android:layout_height="@dimen/layout_height54"
                android:layout_centerInParent="true"
                android:background="@color/gray_dialog" />

            <Button
                android:id="@+id/left_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_toStartOf="@+id/line_vertical"
                android:background="@color/transparent"
                android:text="@string/dialog_no"
                android:textAllCaps="false"
                android:textColor="@color/blue"
                android:textSize="@dimen/text_size17" />
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

在IOS中,背景模糊是非常常見的。想要達(dá)到這一目的,最常見的做法是對此頁面進(jìn)行切圖,在對圖片進(jìn)行模糊。操作實(shí)在麻煩,因此我使用了半透明顏色來代替。當(dāng)然,如果需求確實(shí)需要,也可以做比較復(fù)雜的操作。

<color name="transparent_half">#d9ffffff</color>

邊緣灰色線的shapeshape_frame_gray_background_white如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <corners android:radius="@dimen/radius" />
  <solid android:color="@color/white" />

  <stroke
      android:width="1dp"
      android:color="@color/gray_hint" />
</shape>

布局已經(jīng)Ok,定義仿IOS的dialog文件如下:

public class LikeIosDialog extends Dialog {

  public LikeIosDialog(Context context, int theme) {
      super(context, theme);
  }

  public void onDismiss() {
      if (isShowing()) {
          dismiss();
      }
  }

  public static class Builder {
      private Button leftButton;
      private Button rightButton;
      private TextView dialogTitle;
      private TextView dialogContent;
      private View lineVertical;

      private String message;
      private String title;
      private String leftButtonText;
      private String rightButtonText;
      private View.OnClickListener leftButtonClickListener;
      private View.OnClickListener rightButtonClickListener;
      private boolean isSingle = false;

      private View layout;
      private LikeIosDialog dialog;

      public Builder(Context context) {
          dialog = new LikeIosDialog(context, R.style.Dialog);
          LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          layout = inflater.inflate(R.layout.layout_dialog, null);
          dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
          dialog.setContentView(layout);
          dialog.setCancelable(true);
          dialog.setCanceledOnTouchOutside(false);
      }

      public LikeIosDialog createDialog() {
          initView();
          setText();
          setEvent();
          return dialog;
      }

      private void initView() {
          leftButton = (Button) layout.findViewById(R.id.left_button);
          rightButton = (Button) layout.findViewById(R.id.right_button);
          dialogContent = (TextView) layout.findViewById(R.id.dialog_content);
          dialogTitle = (TextView) layout.findViewById(R.id.dialog_title);
          lineVertical = layout.findViewById(R.id.line_vertical);
      }

      private void setText() {
          if (isSingle) {
              leftButton.setVisibility(View.GONE);
              lineVertical.setVisibility(View.GONE);
          } else {
              leftButton.setVisibility(View.VISIBLE);
              lineVertical.setVisibility(View.VISIBLE);
          }
          dialogContent.setText(message);
          leftButton.setText(leftButtonText);
          rightButton.setText(rightButtonText);
          dialogTitle.setText(title);
      }

      private void setEvent() {
          leftButton.setOnClickListener(leftButtonClickListener);
          rightButton.setOnClickListener(rightButtonClickListener);
      }

      public Builder setMessage(String message) {
          this.message = message;
          return this;
      }

      public Builder setTitle(String title) {
          this.title = title;
          return this;
      }

      public Builder setLeftButton(String leftButtonText, View.OnClickListener leftButtonClickListener) {
          this.leftButtonText = leftButtonText;
          this.leftButtonClickListener = leftButtonClickListener;
          return this;
      }

      public Builder setRightButton(String rightButtonText, View.OnClickListener rightButtonClickListener) {
          this.rightButtonText = rightButtonText;
          this.rightButtonClickListener = rightButtonClickListener;
          return this;
      }

      public Builder isSingleButton(boolean isSingleButton) {
          this.isSingle = isSingleButton;
          return this;
      }

      public Builder setSingleButton(String rightButtonText, View.OnClickListener rightButtonClickListener) {
          this.rightButtonText = rightButtonText;
          this.rightButtonClickListener = rightButtonClickListener;
          return this;
      }
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_BACK) {
          dismiss();
      }
      return super.onKeyDown(keyCode, event);
  }
}

到此,自定義仿IOS的Dialog已經(jīng)完成了,我們簡單的使用一下吧。

private BukkaDialog.Builder deleteDialogBuilder;
private BukkaDialog deleteDialog;

deleteDialogBuilder = new BukkaDialog.Builder(this);
deleteDialog = deleteDialogBuilder
              .setTitle(getString(R.string.dialog_title))
              .setMessage(getString(R.string.dialog_content))
              .isSingleButton(false)
              .setRightButton(getString(R.string.ok), v1 -> {
                 //TODO something
                  deleteDialog.onDismiss();
              })
              .setLeftButton(getString(R.string.cancel), v12 -> deleteDialog.onDismiss())
              .createDialog();
      deleteDialog.show();

一起來看看最終的效果吧,還不錯(cuò)。

最終效果

小結(jié)

這里我只提供了大部分核心代碼,希望對廣大安卓開發(fā)者有所幫助。也希望通過這個(gè)例子,更加清楚的認(rèn)識到彈窗的知識。在這篇文章中,我自定義了一個(gè)仿IOS的Dialog,在android開發(fā)者,自定義View是一塊非常實(shí)用的知識。下一期我想和大家分享的是:Android自定義View。

原文作者litterMay原文鏈接:http://www.itdecent.cn/p/08d17e073318

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,872評論 25 709
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點(diǎn)贊按鈕進(jìn)度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 47,144評論 22 665
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,143評論 4 61
  • 今天是特種兵第四天。今晚簽到創(chuàng)史上新高。我們仿佛已經(jīng)適應(yīng)了這個(gè)節(jié)奏。今天我也主動(dòng)去和顧客朋友溝通。沒想到收獲還是很...
    佳妮520閱讀 116評論 0 0
  • 一、什么是互聯(lián)網(wǎng)思維? 互聯(lián)網(wǎng)+是用互聯(lián)網(wǎng)的思維來做產(chǎn)品, +互聯(lián)網(wǎng)是原來的產(chǎn)品思路加上互聯(lián)網(wǎng)的傳播和營銷功能。這...
    覺醒之旅閱讀 480評論 0 1

友情鏈接更多精彩內(nèi)容