本篇文章給大家分享的是有關(guān)在Android應(yīng)用中常見彈框有哪些,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
10多年的連平網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。營(yíng)銷型網(wǎng)站建設(shè)的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整連平建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“連平網(wǎng)站設(shè)計(jì)”,“連平網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
1.Dialog彈框
android 8種對(duì)話框(Dialog)使用方法匯總
Dialog是系統(tǒng)自帶的彈框,然而常常因?yàn)閁I不好看而遭嫌棄,常需要自定義
public class MyDialog extends Dialog implements android.view.View.OnClickListener { private Context mContext; private String mTitle; private String mDetail; private TextView mTextTitle; private TextView mTextDetail; private TextView mButtonYes; private TextView mButtonNo; private onClickInterface mOnclClickInterface; public MyDialog(Context context, String title, String detail) { super(context, R.style.MyDialogStyle); this.mContext = context; this.mTitle = title; this.mDetail = detail; } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); initView(); } private void initView() { LayoutInflater inflater = LayoutInflater.from(mContext); View view = inflater.inflate(R.layout.layout_dialog, null); setContentView(view); mButtonYes = (TextView) view.findViewById(R.id.dialog_yes); mButtonNo = (TextView) view.findViewById(R.id.dialog_no); mTextTitle = (TextView) view.findViewById(R.id.dialog_title); mTextDetail = (TextView) view.findViewById(R.id.dialog_detail); mTextTitle.setText(mTitle); mTextDetail.setText(mDetail); mButtonYes.setOnClickListener(this); mButtonNo.setOnClickListener(this); } public interface onClickInterface { public void clickYes(); public void clickNo(); } public void setOnClickInterface(onClickInterface onclClickInterface) { this.mOnclClickInterface = onclClickInterface; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.dialog_yes: mOnclClickInterface.clickYes(); break; case R.id.dialog_no: mOnclClickInterface.clickNo(); break; default: break; } } }
這里就是:1.綁定布局 2.設(shè)置監(jiān)聽,監(jiān)聽接口
主要說一下布局里的stytle(懸浮在Activity上,模糊顯示就是在這里控制)
<style name="MyDialogStyle"> <item name="android:windowBackground">@android:color/transparent</item> <!--設(shè)置dialog的背景--> <item name="android:windowFrame">@null</item> <!--Dialog的windowFrame框?yàn)闊o--> <item name="android:windowNoTitle">true</item> <!--是否有title--> <item name="android:windowIsFloating">true</item> <!--是否浮現(xiàn)在activity之上--> <item name="android:windowIsTranslucent">false</item> <!--是否半透明--> <item name="android:windowContentOverlay">@null</item> <!--對(duì)話框是否有遮蓋,這個(gè)不設(shè)置的話,可能會(huì)出現(xiàn)邊框黑線--> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <!--動(dòng)畫--> <item name="android:backgroundDimEnabled">true</item> <!-- 背景是否模糊顯示--> </style>
Dialog彈框一般用于,網(wǎng)絡(luò)數(shù)據(jù)加載顯示,或交互較少的彈框(基本被新下拉刷新控件SwipeRefreshLayout替代)
2.Window彈框
public class MyWindow { private Context mContext; private WindowManager mwinWindowManager; private View mView; private static boolean isShow = false; public MyWindow(Context context) { mContext = context.getApplicationContext(); } public void showMyWindow() { if (isShow) { return; } mwinWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); //設(shè)置WindowManager.LayoutParams的屬性 WindowManager.LayoutParams params = new WindowManager.LayoutParams(); //類型 params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; //flags //如果設(shè)置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,彈出的View沒焦點(diǎn),收不到Back鍵的事件 //當(dāng)按Back、Home鍵時(shí),背景應(yīng)用退出,彈出的view就可以懸浮在桌面了。 params.flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; params.format = PixelFormat.TRANSLUCENT; params.width = LayoutParams.MATCH_PARENT; params.height = LayoutParams.MATCH_PARENT; params.gravity = Gravity.CENTER; //初始化View mView = initView(mContext); //點(diǎn)擊back鍵,關(guān)閉window mView.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { Log.d("wxx", "onKey"); switch (keyCode) { case KeyEvent.KEYCODE_BACK: Log.d("wxx", "onKey BACK"); hideMyWindow(); return true; default: return false; } } }); mwinWindowManager.addView(mView, params); isShow = true; } private View initView(Context context) { LayoutInflater inflater = LayoutInflater.from(context); View view = inflater.inflate(R.layout.layout_window, null); Button btnYes = (Button) view.findViewById(R.id.window_yes); btnYes.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext, "Window yes!", Toast.LENGTH_LONG).show(); hideMyWindow(); } }); Button btnNO = (Button) view.findViewById(R.id.window_no); btnNO.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext, "Window No!", Toast.LENGTH_LONG).show(); hideMyWindow(); } }); //點(diǎn)擊window窗口外圍,關(guān)閉window final View wView = view.findViewById(R.id.view_layout); view.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int x = (int) event.getX(); int y = (int) event.getY(); Rect rect = new Rect(); wView.getGlobalVisibleRect(rect); if (!rect.contains(x, y)) { hideMyWindow(); } return false; } }); return view; } public void hideMyWindow() { if (isShow && mView != null) { mwinWindowManager.removeView(mView); isShow = false; } } }
這個(gè)就厲害了,展現(xiàn),隱藏,點(diǎn)擊監(jiān)聽都是自己寫的方法,原來點(diǎn)擊窗口外關(guān)閉窗口是這樣實(shí)現(xiàn)的,是不是又學(xué)到了
然后是關(guān)于:
android Window WindowManager 整理
主要用于自定義控件,比如說下拉框:
自定義spinner下拉框
3.Activity偽彈框
public class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.layout_activity); } @Override public boolean onTouchEvent(MotionEvent event) { this.finish(); return true; } public void YesButton(View v) { this.finish(); Toast.makeText(getApplicationContext(), "Yes, Hunman is Activity", Toast.LENGTH_LONG).show(); } public void NoButton(View v) { this.finish(); Toast.makeText(getApplicationContext(), "No, Hunman is not Activity", Toast.LENGTH_LONG).show(); } }
簡(jiǎn)單得不能再簡(jiǎn)單,跳轉(zhuǎn)跳轉(zhuǎn)到另一個(gè)Activity
注意點(diǎn)Activity主題(控制Activity為彈框樣式):
<activity android:name="com.wuxianxi.hunman.smallwindows.MyActivity" android:theme="@style/MyActivityStyle" > </activity> <style name="MyActivityStyle"> <item name="android:windowBackground">@android:color/transparent</item> <!--設(shè)置dialog的背景--> <item name="android:windowFrame">@null</item> <!--Dialog的windowFrame框?yàn)闊o--> <item name="android:windowNoTitle">true</item> <!--是否有title--> <item name="android:windowIsFloating">true</item> <!--是否浮現(xiàn)在activity之上--> <item name="android:windowIsTranslucent">true</item> <!--是否半透明--> <item name="android:windowContentOverlay">@null</item> <!--對(duì)話框是否有遮蓋,這個(gè)不設(shè)置的話,可能會(huì)出現(xiàn)邊框黑線--> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <!--動(dòng)畫--> <item name="android:backgroundDimEnabled">true</item> <!-- 背景是否模糊顯示--> </style>
Activity偽彈框最常用,用于交互,操作較復(fù)雜的彈框,如圖二
補(bǔ)充這里可通過方法控制Activity彈框?qū)挶?/p>
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /** 設(shè)置寬度為屏幕的0.9*/ WindowManager windowManager = getWindowManager(); /* 獲取屏幕寬、高 */ Display display = windowManager.getDefaultDisplay(); /* 獲取對(duì)話框當(dāng)前的參數(shù)值 */ WindowManager.LayoutParams p = getWindow().getAttributes(); /* 寬度設(shè)置為屏幕的0.9 */ p.width = (int) (display.getWidth() * 0.9); /* 設(shè)置透明度,0.0為完全透明,1.0為完全不透明 */ p.alpha = 0.95f; /* 設(shè)置布局參數(shù) */ getWindow().setAttributes(p); // getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, // ViewGroup.LayoutParams.WRAP_CONTENT); /* 設(shè)置點(diǎn)擊彈框外部不可消失 */ setFinishOnTouchOutside(false); }
最后貼上MainActivity
public class MainActivity extends ActionBarActivity implements OnClickListener { private Button btnWindow; private Button btnDialog; private Button btnActivity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { btnWindow = (Button) findViewById(R.id.btn_window); btnDialog = (Button) findViewById(R.id.btn_dialog); btnActivity = (Button) findViewById(R.id.btn_activity); btnWindow.setOnClickListener(this); btnDialog.setOnClickListener(this); btnActivity.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_window: MyWindow myWindow = new MyWindow(this); myWindow.showMyWindow(); break; case R.id.btn_dialog: //注意下面第一個(gè)參數(shù)不能為getApplicationContext(),而應(yīng)該是Activity, 因?yàn)檗k有activity才能添加窗口 final MyDialog dialog = new MyDialog(MainActivity.this, "Hunman - Dialog", "Hunman is a Dialog\nYes or No!"); dialog.show(); dialog.setOnClickInterface(new MyDialog.onClickInterface() { @Override public void clickYes() { dialog.dismiss(); Toast.makeText(getApplicationContext(), "Yes, Hunman is Dialog", Toast.LENGTH_LONG).show(); } @Override public void clickNo() { dialog.dismiss(); Toast.makeText(getApplicationContext(), "Yes, Hunman is not Dialog", Toast.LENGTH_LONG).show(); } }); break; case R.id.btn_activity: Intent intent3 = new Intent(MainActivity.this, MyActivity.class); startActivity(intent3); break; default: break; } } }
以上就是在Android應(yīng)用中常見彈框有哪些,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享標(biāo)題:在Android應(yīng)用中常見彈框有哪些
轉(zhuǎn)載來源:http://www.rwnh.cn/article44/pcopee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、微信公眾號(hào)、網(wǎng)站制作、面包屑導(dǎo)航、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)