Android controls encapsulate self-encapsulated dialog controls

Source: Internet
Author: User

Custom dialog is certainly a lot of use, but it feels like every execution is messy and simple to complete the task. Now, after encapsulating it, you can use direct copy.
First:

Main activity Copy codeThe Code is as follows: package com. su. testcustomdialog;
Import com. su. testcustomdialog. MyDialog. Dialogcallback;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. TextView;
Public class CustomDialogActivity extends Activity {
Private TextView textView;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TextView = (TextView) findViewById (R. id. textView11 );
Button button = (Button) findViewById (R. id. button1 );
Button. setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View v ){
MyDialog myDialog = new MyDialog (CustomDialogActivity. this );
MyDialog. setContent ("brother from Activity ");
MyDialog. setDialogCallback (dialogcallback );
MyDialog. show ();
}
});
}
/**
* Set things to be handled by mydialog
*/
Dialogcallback dialogcallback = new Dialogcallback (){
@ Override
Public void dialogdo (String string ){
TextView. setText ("brother from Dialog:" + string );
}
};
}

And then the core of MyDialog.Copy codeThe Code is as follows: package com. su. testcustomdialog;
Import android. app. Dialog;
Import android. content. Context;
Import android. view. View;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. TextView;
/**
* Custom dialog
* @ Author sfshine
*
*/
Public class MyDialog {
Context context;
Dialogcallback dialogcallback;
Dialog dialog;
Button sure;
TextView textView;
EditText editText;
/**
* Init the dialog
* @ Return
*/
Public MyDialog (Context con ){
This. context = con;
Dialog = new Dialog (context, R. style. dialog );
Dialog. setContentView (R. layout. dialog );
TextView = (TextView) dialog. findViewById (R. id. textview );
Sure = (Button) dialog. findViewById (R. id. button1 );
EditText = (EditText) dialog. findViewById (R. id. editText1 );
Sure. setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View v ){
Dialogcallback. dialogdo (editText. getText (). toString ());
Dismiss ();
}
});
}
/**
* Set an interfack interface so that mydialog can handle things defined by activity.
* @ Author sfshine
*
*/
Public interface Dialogcallback {
Public void dialogdo (String string );
}
Public void setDialogCallback (Dialogcallback dialogcallback ){
This. dialogcallback = dialogcallback;
}
/**
* @ Category Set The Content of the TextView
**/
Public void setContent (String content ){
TextView. setText (content );
}
/**
* Get the Text of the EditText
**/
Public String getText (){
Return editText. getText (). toString ();
}
Public void show (){
Dialog. show ();
}
Public void hide (){
Dialog. hide ();
}
Public void dismiss (){
Dialog. dismiss ();
}
}

Dialog LayoutCopy codeThe Code is as follows: <? 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 = "wrap_content"
Android: layout_margin = "30366dp"
Android: orientation = "vertical"
Android: padding = "10dip">
<! -- If android: layout_width = "5000dip" is used to set a very large value system, -->
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_margin = "30366dp"
Android: gravity = "center"
Android: text = "Custom Dialog"
Android: textColor = "# F0F"
Android: textSize = "20dip"/>
<TextView
Android: id = "@ + id/textview"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<EditText
Android: id = "@ + id/editText1"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: EMS = "10">
</EditText>
<Button
Android: id = "@ + id/button1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "right"
Android: text = "Confirm"/>
</LinearLayout>

Style FileCopy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "dialog" parent = "@ android: style/Theme. Dialog">
<Item name = "android: windowFrame"> @ null </item>
<Item name = "android: incluwisfloating"> true </item>
<Item name = "android: javaswistranslucent"> false </item>
<Item name = "android: windowNoTitle"> true </item>
<Item name = "android: background"> # FFF </item>
<Item name = "android: windowBackground"> @ android: color/transparent </item>
<Item name = "android: backgroundDimEnabled"> true </item>
</Style>
</Resources>

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.