歡迎您光臨本站 註冊首頁

Android 開發使用PopupWindow實現彈出警告框的複用類示例

←手機掃碼閱讀     zhang3221994 @ 2020-06-09 , reply:0

本文實例講述了Android 開發使用PopupWindow實現彈出警告框的複用類。分享給大家供大家參考,具體如下:

Android開發中相信下圖所示界面大家都不陌生,該種彈出框的使用頻率也是極高的,所以我專門謝了個類用於方便的彈出該界面。並把確定或取消後的邏輯通過抽象方法的方式讓用戶自己實現,大大提高了開發效率。下面是該類:

  package com.***.popupwindow;    import ******;    public abstract class MyPopupWindow {      private PopupWindow popupWindow;    private Activity context;    private String content;    private String positiveWord = "確定";    private String negativeWord = "取消";      /**     * 構造函數     *     * @param context     */    public MyPopupWindow(Activity context) {      this.context = context;    }      /**     * 顯示警示框     */    public void show() {      View popView = View.inflate(context, R.layout.popup, null);      popupWindow = new PopupWindow(context);      popupWindow.setHeight(400);      popupWindow.setWidth(700);      popupWindow.setOutsideTouchable(true);      popupWindow.setFocusable(true);      popupWindow.setContentView(popView);      popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER, 0, 0);        TextView tv_pop_text = (TextView) popView.findViewById(R.id.tv_pop_text);      tv_pop_text.setText(content);        Button bt_pop_sure = (Button) popView.findViewById(R.id.bt_pop_sure);      bt_pop_sure.setText(positiveWord);      bt_pop_sure.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {          sureClick();        }      });        Button bt_pop_cancel = (Button) popView.findViewById(R.id.bt_pop_cancel);      bt_pop_cancel.setText(negativeWord);      bt_pop_cancel.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {          cancelClick();        }      });    }      /**     * 確定鍵按下後執行     */    public abstract void sureClick();      /**     * 取消鍵按下後執行     */    public abstract void cancelClick();      /**     * 為警示設置警示內容     *     * @param content     */    public void setContent(String content) {      this.content = content;    }      /**     * 設置確定鍵文字     *     * @param positiveWord     */    public void setPositiveWord(String positiveWord) {      this.positiveWord = positiveWord;    }      /**     * 設置取消鍵文字     *     * @param negativeWord     */    public void setNegativeWord(String negativeWord) {      this.negativeWord = negativeWord;    }      /**     * 手動取消警示框     */    public void dismiss() {      popupWindow.dismiss();    }  }

 

其中彈出框用到的佈局popup.xml代碼如下:

  

 

下面簡單的使用一下:在界面放一個按鈕,按鈕點擊後彈出警告框。代碼如下:

  package com.toprs.popupwindow;    import android.graphics.Color;  import android.graphics.drawable.ColorDrawable;  import android.support.v7.app.AppCompatActivity;  import android.os.Bundle;  import android.util.AttributeSet;  import android.view.Gravity;  import android.view.LayoutInflater;  import android.view.View;  import android.view.ViewGroup;  import android.widget.ArrayAdapter;  import android.widget.Button;  import android.widget.ListView;  import android.widget.PopupWindow;  import android.widget.SeekBar;  import android.widget.Toast;    public class MainActivity extends AppCompatActivity {      private PopupWindow popupWindow;      private Button button;      @Override    protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);        button = (Button) findViewById(R.id.button);      button.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {          MyPopupWindow myPopupWindow = new MyPopupWindow(MainActivity.this) {              @Override            public void sureClick() {              Toast.makeText(MainActivity.this, "確定", Toast.LENGTH_SHORT).show();            }              @Override            public void cancelClick() {              Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();            }          };          myPopupWindow.setContent("確定退出?");          myPopupWindow.show();        }      });    }  }

 

即如下效果:

So,以後使用只需要簡單調用幾句代碼就好了!


   


[zhang3221994 ] Android 開發使用PopupWindow實現彈出警告框的複用類示例已經有254次圍觀

http://coctec.com/docs/android/show-post-237610.html