您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關在Android應用中常見彈框有哪些,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
1.Dialog彈框
android 8種對話框(Dialog)使用方法匯總
Dialog是系統自帶的彈框,然而常常因為UI不好看而遭嫌棄,常需要自定義
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.設置監聽,監聽接口
主要說一下布局里的stytle(懸浮在Activity上,模糊顯示就是在這里控制)
<style name="MyDialogStyle"> <item name="android:windowBackground">@android:color/transparent</item> <!--設置dialog的背景--> <item name="android:windowFrame">@null</item> <!--Dialog的windowFrame框為無--> <item name="android:windowNoTitle">true</item> <!--是否有title--> <item name="android:windowIsFloating">true</item> <!--是否浮現在activity之上--> <item name="android:windowIsTranslucent">false</item> <!--是否半透明--> <item name="android:windowContentOverlay">@null</item> <!--對話框是否有遮蓋,這個不設置的話,可能會出現邊框黑線--> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <!--動畫--> <item name="android:backgroundDimEnabled">true</item> <!-- 背景是否模糊顯示--> </style>
Dialog彈框一般用于,網絡數據加載顯示,或交互較少的彈框(基本被新下拉刷新控件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); //設置WindowManager.LayoutParams的屬性 WindowManager.LayoutParams params = new WindowManager.LayoutParams(); //類型 params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; //flags //如果設置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,彈出的View沒焦點,收不到Back鍵的事件 //當按Back、Home鍵時,背景應用退出,彈出的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); //點擊back鍵,關閉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(); } }); //點擊window窗口外圍,關閉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; } } }
這個就厲害了,展現,隱藏,點擊監聽都是自己寫的方法,原來點擊窗口外關閉窗口是這樣實現的,是不是又學到了
然后是關于:
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(); } }
簡單得不能再簡單,跳轉跳轉到另一個Activity
注意點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> <!--設置dialog的背景--> <item name="android:windowFrame">@null</item> <!--Dialog的windowFrame框為無--> <item name="android:windowNoTitle">true</item> <!--是否有title--> <item name="android:windowIsFloating">true</item> <!--是否浮現在activity之上--> <item name="android:windowIsTranslucent">true</item> <!--是否半透明--> <item name="android:windowContentOverlay">@null</item> <!--對話框是否有遮蓋,這個不設置的話,可能會出現邊框黑線--> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <!--動畫--> <item name="android:backgroundDimEnabled">true</item> <!-- 背景是否模糊顯示--> </style>
Activity偽彈框最常用,用于交互,操作較復雜的彈框,如圖二
補充這里可通過方法控制Activity彈框寬比
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /** 設置寬度為屏幕的0.9*/ WindowManager windowManager = getWindowManager(); /* 獲取屏幕寬、高 */ Display display = windowManager.getDefaultDisplay(); /* 獲取對話框當前的參數值 */ WindowManager.LayoutParams p = getWindow().getAttributes(); /* 寬度設置為屏幕的0.9 */ p.width = (int) (display.getWidth() * 0.9); /* 設置透明度,0.0為完全透明,1.0為完全不透明 */ p.alpha = 0.95f; /* 設置布局參數 */ getWindow().setAttributes(p); // getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, // ViewGroup.LayoutParams.WRAP_CONTENT); /* 設置點擊彈框外部不可消失 */ 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: //注意下面第一個參數不能為getApplicationContext(),而應該是Activity, 因為辦有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應用中常見彈框有哪些,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。