Android IOS-like UIAlertView dialog box

Source: Internet
Author: User

Display Effect:

I saw the author's imitation qq prompt box in the reference link, but it was not very helpful and there were some shortcomings. So I referred to the Android system AlertDialog, use the layout file and style file in the reference link to customize the effect of the UIAlertView on IOS in your own way, in this way, we can use custom mmdialog like AlertDialog.
CustomDialog use code:

package com.example.iosalertview;import android.app.Activity;import android.app.AlertDialog;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import com.example.iosalertview.CustomDialog.Builder;public class MainActivity extends Activity implements OnClickListener{private Button ios_dialog_btn,android_dialog_btn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ios_dialog_btn = (Button) findViewById(R.id.ios_dialog_btn);android_dialog_btn = (Button) findViewById(R.id.android_dialog_btn);ios_dialog_btn.setOnClickListener(this);android_dialog_btn.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.ios_dialog_btn:CustomDialog.Builder builder = new Builder(MainActivity.this);builder.setTitle(R.string.prompt);builder.setMessage(R.string.exit_app);builder.setPositiveButton(R.string.confirm, null);builder.setNegativeButton(R.string.cancel, null);builder.create().show();break;case R.id.android_dialog_btn:AlertDialog.Builder mbuilder = new AlertDialog.Builder(MainActivity.this);mbuilder.setTitle(R.string.prompt);mbuilder.setMessage(R.string.exit_app);mbuilder.setPositiveButton(R.string.confirm, null);mbuilder.setNegativeButton(R.string.cancel, null);mbuilder.create().show();break;default:break;}}}

Custom mmdialog code:
Package com. example. iosalertview; import android. app. dialog; import android. content. context; import android. content. dialogInterface; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup. layoutParams; import android. widget. button; import android. widget. linearLayout; import android. widget. textView; public class CustomDialog extends Dialog {public CustomDialog (Context cont Ext) {super (context);} public CustomDialog (Context context, int theme) {super (context, theme);} public static class Builder {private Context context; // context object private String title; // dialog box title private String message; // dialog box content private String confirm_btnText; // The button name "OK" private String cancel_btnText; // The button name "cancel" private View contentView; // other layout interfaces loaded in the middle of the dialog box/* button strength event */private DialogInterface. onClickListener confirm_bt NClickListener; private DialogInterface. onClickListener cancel_btnClickListener; public Builder (Context context) {this. context = context;}/* set the dialog box information */public Builder setMessage (String message) {this. message = message; return this;}/*** Set the Dialog message from resource ** @ param title * @ return */public Builder setMessage (int message) {this. message = (String) context. getText (message); return this; }/*** Set the Dialog title from resource ** @ param title * @ return */public Builder setTitle (int title) {this. title = (String) context. getText (title); return this;}/*** Set the Dialog title from String ** @ param title * @ return */public Builder setTitle (String title) {this. title = title; return this;}/*** dialog box * @ param v View * @ return */public Builder setContentView (View v) {this. contentVie W = v; return this;}/*** Set the positive button resource and it's listener ** @ param confirm_btnText * @ return */public Builder setPositiveButton (int confirm_btnText, dialogInterface. onClickListener listener) {this. confirm_btnText = (String) context. getText (confirm_btnText); this. confirm_btnClickListener = listener; return this;}/*** Set the positive button and it's listener ** @ param confirm_btn Text * @ return */public Builder setPositiveButton (String confirm_btnText, DialogInterface. onClickListener listener) {this. confirm_btnText = confirm_btnText; this. confirm_btnClickListener = listener; return this;}/*** Set the negative button resource and it's listener ** @ param confirm_btnText * @ return */public Builder setNegativeButton (int cancel_btnText, dialogInterface. onClickListener listener ){ This. cancel_btnText = (String) context. getText (cancel_btnText); this. cancel_btnClickListener = listener; return this;}/*** Set the negative button and it's listener ** @ param confirm_btnText * @ return */public Builder setNegativeButton (String cancel_btnText, DialogInterface. onClickListener listener) {this. cancel_btnText = cancel_btnText; this. cancel_btnClickListener = listener; return this;} public Cu StomDialog create () {LayoutInflater inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); // instantiate the dialog with the custom Themefinal CustomDialog dialog = new CustomDialog (context, R. style. mystyle); View layout = inflater. inflate (R. layout. customdialog, null); dialog. addContentView (layout, new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT) ); // Set the dialog title (TextView) layout. findViewById (R. id. title )). setText (title); (TextView) layout. findViewById (R. id. title )). getPaint (). setFakeBoldText (true); // set the confirm buttonif (confirm_btnText! = Null) {(Button) layout. findViewById (R. id. confirm_btn). setText (confirm_btnText); if (confirm_btnClickListener! = Null) {(Button) layout. findViewById (R. id. confirm_btn )). setOnClickListener (new View. onClickListener () {public void onClick (View v) {confirm_btnClickListener.onClick (dialog, DialogInterface. BUTTON_POSITIVE) ;}}}} else {// if no confirm button just set the visibility to GONElayout. findViewById (R. id. confirm_btn ). setVisibility (View. GONE);} // set the cancel buttonif (cancel_btnText! = Null) {(Button) layout. findViewById (R. id. cancel_btn). setText (cancel_btnText); if (cancel_btnClickListener! = Null) {(Button) layout. findViewById (R. id. cancel_btn )). setOnClickListener (new View. onClickListener () {public void onClick (View v) {cancel_btnClickListener.onClick (dialog, DialogInterface. BUTTON_NEGATIVE) ;}} else {// if no confirm button just set the visibility to GONElayout. findViewById (R. id. cancel_btn ). setVisibility (View. GONE);} // set the content messageif (message! = Null) {(TextView) layout. findViewById (R. id. message). setText (message);} else if (contentView! = Null) {// if no message set // add the contentView to the dialog body (LinearLayout) layout. findViewById (R. id. message )). removeAllViews (); (LinearLayout) layout. findViewById (R. id. message )). addView (contentView, new LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT);} dialog. setContentView (layout); return dialog ;}}}

Demo: http://download.csdn.net/detail/zhufuing/6880735


Reference connection:

Http://www.apkbus.com/forum.php? Mod = viewthread & tid = 160284.

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.