Android implements a pop-up box that can enter data _android

Source: Internet
Author: User
Tags event listener gettext trim

A previous article that describes how to define a pop-up popupwindow from the bottom of the screen-- "Android animation the bottom of the actual screen pop-up Popupwindow,"and then suddenly remember the pop-up box that was previously written with the custom content. I wrote two examples and shared them:

The first implementation method: Inherit Dialog
1.1 line defines what the pop-up box will display: Create_user_dialog.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:id=" @+id/create_user_dialog_view "android:layout_width=" fill_parent "android:layout_height=" wrap _content "android:background=" @drawable/dialog_load_bg "android:minwidth=" 200DP "android:orientation=" vertical "an droid:padding= "10DP" android:paddingbottom= "30DP" android:paddingtop= "30DP" > <edittext android:id= "@+id/t" Ext_name "android:layout_width=" fill_parent "android:layout_height=" wrap_content "android:background=" @drawable/e DIT_BG "android:hint=" name "android:minheight=" 45DP "android:textsize=" 18sp "/>" <edittext android:id= " @+id/text_mobile "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "Android:layout_marginTo" p= "5DP" android:background= "@drawable/edit_bg" android:hint= "mobile number" android:minheight= "45DP" android:textsize= "1 8sp "/> <edittext AndrOid:id= "@+id/text_info" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:layout_ma rgintop= "5DP" android:background= "@drawable/edit_bg" android:gravity= "Top|left" android:hint= "Personality signature" Android:mi nheight= "145DP" android:textsize= "18sp"/> <button android:id= "@+id/btn_save_pop" android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_margintop= "5DP" android:text= "Save"/> < 
 /linearlayout>

The

 1.2 defines the dialog

to eject

public class Createuserdialog extends Dialog {/** * Contextual Object * * */activity context; 
 
 Private Button Btn_save; 
 
 Public EditText Text_name; 
 
 Public EditText Text_mobile; 
 
 
 Public EditText Text_info; 
 
 Private View.onclicklistener Mclicklistener; 
  Public Createuserdialog {Super (context); 
 This.context = context;  
  Public Createuserdialog (activity context, int theme, View.onclicklistener Clicklistener) {Super (context, theme); 
  This.context = context; 
 This.mclicklistener = Clicklistener; 
  } @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 
  Specifies the layout this.setcontentview (R.layout.create_user_dialog); 
  Text_name = (edittext) Findviewbyid (r.id.text_name); 
  Text_mobile = (edittext) Findviewbyid (r.id.text_mobile); 
 
  Text_info = (edittext) Findviewbyid (r.id.text_info); * * Get the Window object and parameter object of the Christmas box to modify the layout settings of the dialog box, you can call GetWindow () directly, which means that you get the Windows object for this activity, so thisThe properties of this activity can be changed in the same way. 
 
  * * Window Dialogwindow = This.getwindow (); 
  WindowManager m = Context.getwindowmanager (); Display d = m.getdefaultdisplay (); Get screen width, high use windowmanager.layoutparams p = dialogwindow.getattributes (); Gets the current parameter value of the dialog box//p.height = (int) (D.getheight () * 0.6); Height is set to screen 0.6 p.width = (int) (D.getwidth () * 0.8); 
 
  The width is set to the 0.8 dialogwindow.setattributes (p) of the screen; 
 
  Locate the Control object Btn_save = (Button) Findviewbyid (r.id.btn_save) in the layout based on the ID; 
 
  Click the event Listener Btn_save.setonclicklistener (mclicklistener) for button binding; 
 This.setcancelable (TRUE); 
 } 
}

1.3 Invoke Pop-up Box

public void Showeditdialog (view view) { 
 Createuserdialog = new Createuserdialog (This,r.style.loading_dialog, Onclicklistener); 
 Createuserdialog.show (); 
} 



Private View.onclicklistener Onclicklistener = new View.onclicklistener () { 
  @Override public 
  void OnClick (View V) { 
 
   switch (V.getid ()) {case 
    r.id.btn_save: 
 
     String name = CreateUserDialog.text_name.getText (). ToString (). Trim (); 
     String mobile = CreateUserDialog.text_mobile.getText (). toString (). Trim (); 
     String info = CreateUserDialog.text_info.getText (). toString (). Trim (); 
 
     System.out.println (name+ "-+mobile+"-"+info"); 
     break; 
   } 
  } 
 ; 

The second way to implement it: inherit Popupwindow
2.1 Definition Popup layout file, and 1.1 definition consistent
2.2 Definition of Popupwindow to eject

public class Createuserpopwin extends Popupwindow {private context mcontext; 
 
 Private view view; 
 
 Private Button Btn_save_pop; 
 
 Public EditText Text_name; 
 
 Public EditText Text_mobile; 
 
 
 Public EditText Text_info; 
 
  Public Createuserpopwin (Activity mcontext, View.onclicklistener itemsonclick) {this.mcontext = Mcontext; 
 
  This.view = Layoutinflater.from (Mcontext). Inflate (R.layout.create_user_pop, NULL); 
  Text_name = (edittext) View.findviewbyid (r.id.text_name); 
  Text_mobile = (edittext) View.findviewbyid (r.id.text_mobile); 
 
  Text_info = (edittext) View.findviewbyid (r.id.text_info); 
 
  Btn_save_pop = (Button) View.findviewbyid (R.id.btn_save_pop); 
 
  Set button to monitor Btn_save_pop.setonclicklistener (Itemsonclick); 
 
 
  Set external clickable this.setoutsidetouchable (true); 
 
  /* Set pop-up window features//Set view This.setcontentview (This.view); Set pop-up form width and Height * * Get the Window object and parameter object of the Christmas box to modify the layout settings of the dialog box, you can call GetWindow () directly to get the window object for this activity, so that it can be changed in the same wayChange the properties of this activity. 
 
  * * Window Dialogwindow = Mcontext.getwindow (); 
  WindowManager m = Mcontext.getwindowmanager (); Display d = m.getdefaultdisplay (); Get screen width, high use windowmanager.layoutparams p = dialogwindow.getattributes (); 
  Gets the current parameter value of the dialog box This.setheight (RelativeLayout.LayoutParams.WRAP_CONTENT); 
 
  This.setwidth ((int) (D.getwidth () * 0.8)); 
 
 Set pop-up form to click This.setfocusable (TRUE); 
 } 
}

2.3 Calling the Frame component

public void Showeditpopwin (view view) { 
 
  Createuserpopwin = new Createuserpopwin (this,onclicklistener); 
 
  Createuserpopwin.showatlocation (Findviewbyid (R.id.main_view), Gravity.center, 0, 0); 


Private View.onclicklistener Onclicklistener = new View.onclicklistener () { 
  @Override public 
  void OnClick (View V) { 
 
   switch (V.getid ()) {case 
    R.id.btn_save_pop: 
 
     String name1 = CreateUserPopWin.text_name.getText (). ToString (). Trim (); 
     String mobile1 = CreateUserPopWin.text_mobile.getText (). toString (). Trim (); 
     String info1 = CreateUserPopWin.text_info.getText (). toString (). Trim (); 
 
     System.out.println (name1+ "-+mobile1+"-"+info1"); 
 
     Createuserpopwin.dismiss (); 
     break; 
   } 
  } 
 ; 

This is the entire content of this article, I hope to learn more about Android to help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.