[Android] exercise PopupWindow implementation dialog box, androidpopupwindow
Exercise usage Dialog internship Dialog box
Package com. example. tsh; import android. app. activity; import android. app. dialog; import android. OS. bundle; import android. OS. handler; import android. view. motionEvent; import android. view. view; import android. view. view. onClickListener; import android. view. view. onTouchListener; import android. view. viewGroup. layoutParams; import android. widget. button; import android. widget. popupWindow; public class MainActivity extends Activity {private Handler handler = new Handler (); @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // Dialog implementation Dialog box dialog Dialog = new Dialog (this, R. style. theme_Tsh_Dialog); dialog. setContentView (R. layout. activity_two); dialog. show ();}}
Styles. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Resources xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- Custom dialog box topic --> <style name = "Theme. tsh. dialog "parent =" @ android: style/Theme. dialog "> <item name =" android: windowBackground "> @ null </item> </style> </resources>
Exercise Using PopupWindow implementation dialog box
Package com. example. tsh; import android. app. activity; import android. app. dialog; import android. OS. bundle; import android. OS. handler; import android. view. motionEvent; import android. view. view; import android. view. view. onClickListener; import android. view. view. onTouchListener; import android. view. viewGroup. layoutParams; import android. widget. button; import android. widget. popupWindow; public class MainActivity extends Activity {private Handler handler = new Handler (); @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // Dialog implementation Dialog box // dialog Dialog = new Dialog (this, R. style. theme_Tsh_Dialog); // dialog. setContentView (R. layout. activity_two); // dialog. show (); // use PopupWindow to implement the dialog box Button dropDown = (Button) findViewById (R. id. bt_dropdown); dropDown. setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {View anchor = View. inflate (MainActivity. this, R. layout. activity_two, null); final PopupWindow popupWindow = new PopupWindow (anchor, LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT, true); // click a blank area to hide the popupWindow. setTouchable (true); popupWindow. setTouchInterceptor (new OnTouchListener () {@ Override public boolean onTouch (View arg0, MotionEvent arg1) {popupWindow. dismiss (); return false ;}}); popupWindow. setBackgroundDrawable (getResources (). getDrawable (R. drawable. ic_launcher); popupWindow. showAsDropDown (arg0 );}});}}
Activity_main.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: orientation = "vertical"> <TextView android: layout_width = "match_parent" android: layout_height = "wrap_content" android: text = "first interface"/> <Button android: id = "@ + id/bt_dropdown" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "display drop-down"/> </LinearLayout>