色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Android 開(kāi)發(fā)使用PopupWindow實(shí)現(xiàn)彈出警告框的復(fù)用類(lèi)示例

瀏覽:44日期:2022-09-24 10:02:39

本文實(shí)例講述了Android 開(kāi)發(fā)使用PopupWindow實(shí)現(xiàn)彈出警告框的復(fù)用類(lèi)。分享給大家供大家參考,具體如下:

Android開(kāi)發(fā)中相信下圖所示界面大家都不陌生,該種彈出框的使用頻率也是極高的,所以我專門(mén)謝了個(gè)類(lèi)用于方便的彈出該界面。并把確定或取消后的邏輯通過(guò)抽象方法的方式讓用戶自己實(shí)現(xiàn),大大提高了開(kāi)發(fā)效率。下面是該類(lèi):

Android 開(kāi)發(fā)使用PopupWindow實(shí)現(xiàn)彈出警告框的復(fù)用類(lèi)示例

package com.***.popupwindow;import ******;public abstract class MyPopupWindow { private PopupWindow popupWindow; private Activity context; private String content; private String positiveWord = '確定'; private String negativeWord = '取消'; /** * 構(gòu)造函數(shù) * * @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(); } }); } /** * 確定鍵按下后執(zhí)行 */ public abstract void sureClick(); /** * 取消鍵按下后執(zhí)行 */ public abstract void cancelClick(); /** * 為警示設(shè)置警示內(nèi)容 * * @param content */ public void setContent(String content) { this.content = content; } /** * 設(shè)置確定鍵文字 * * @param positiveWord */ public void setPositiveWord(String positiveWord) { this.positiveWord = positiveWord; } /** * 設(shè)置取消鍵文字 * * @param negativeWord */ public void setNegativeWord(String negativeWord) { this.negativeWord = negativeWord; } /** * 手動(dòng)取消警示框 */ public void dismiss() { popupWindow.dismiss(); }}

其中彈出框用到的布局popup.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:background='@android:color/white' android:orientation='vertical'> <TextView android: android:layout_width='match_parent' android:layout_height='wrap_content' android:layout_weight='1' android:gravity='center'/> <TextView android:layout_width='match_parent' android:layout_height='1px' android:background='@android:color/darker_gray'/> <LinearLayout android:layout_width='match_parent' android:layout_height='wrap_content' android:orientation='horizontal'> <Button android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:background='@android:color/transparent' android:layout_weight='1'/> <TextView android:layout_width='1px' android:layout_height='match_parent' android:background='@android:color/darker_gray'/> <Button android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:background='@android:color/transparent' android:layout_weight='1'/> </LinearLayout></LinearLayout>

下面簡(jiǎn)單的使用一下:在界面放一個(gè)按鈕,按鈕點(diǎn)擊后彈出警告框。代碼如下:

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(); } }); }}

即如下效果:

Android 開(kāi)發(fā)使用PopupWindow實(shí)現(xiàn)彈出警告框的復(fù)用類(lèi)示例

So,以后使用只需要簡(jiǎn)單調(diào)用幾句代碼就好了!

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》及《Android資源操作技巧匯總》

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: www.av在线 | 美女与男人对肌免费网站 | 国产精品免费久久 | 免费欧美黄色 | 51国产偷自视频区视频手机播器 | 亚洲品质自拍 | 日本高清视频一区二区 | 在线亚洲一区二区 | 日韩美三级| 国产精品久久久久久久毛片 | 毛片大全在线观看 | 欧美午夜视频一区二区三区 | 亚洲天堂久久精品 | 久久国产网 | 久在线| 成年人三级黄色片 | 精品国产看高清国产毛片 | 日韩在线专区 | 国产精品19禁在线观看2021 | 国产片在线观看狂喷潮bt天堂 | 亚洲色在线视频 | 亚洲伦乱| 国产在线综合一区二区三区 | 欧美大屁股精品毛片视频 | 中文字幕亚洲一区二区va在线 | 国产毛片久久久久久国产毛片 | 国产一区二区三区在线看 | 在线精品欧美日韩 | 激情性爽三级成人 | 免费看亚洲| 91视频最新网站 | 亚洲成人免费在线 | 国产一区免费在线观看 | 男人的天堂在线 | 国产伦精一区二区三区 | 三级黄色片网站 | 午夜黄色福利视频 | 国产成人精品一区二区免费 | 色综合91久久精品中文字幕 | 黄网免费 | 国内精品免费一区二区观看 |