温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中怎么实现一个IOS UIAlertView对话框

发布时间:2021-06-11 14:36:16 来源:亿速云 阅读:169 作者:Leah 栏目:移动开发

Android中怎么实现一个IOS UIAlertView对话框?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

CustomDialog使用代码:

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; 
 
 @Override 
 protected 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); 
   
 } 
 
 @Override 
 public 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; 
  } 
 } 
 
}

自定义CustomDialog代码:

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 context) { 
  super(context); 
 } 
 
 public CustomDialog(Context context, int theme) { 
  super(context, theme); 
 } 
 
 public static class Builder { 
  private Context context; //上下文对象 
  private String title; //对话框标题 
  private String message; //对话框内容 
  private String confirm_btnText; //按钮名称“确定” 
  private String cancel_btnText; //按钮名称“取消” 
  private View contentView; //对话框中间加载的其他布局界面 
  /*按钮坚挺事件*/ 
  private DialogInterface.OnClickListener confirm_btnClickListener; 
  private DialogInterface.OnClickListener cancel_btnClickListener; 
 
  public Builder(Context context) { 
   this.context = context; 
  } 
 
  /*设置对话框信息*/ 
  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; 
  } 
 
  /** 
   * 设置对话框界面 
   * @param v View 
   * @return 
   */ 
  public Builder setContentView(View v) { 
   this.contentView = 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_btnText 
   * @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 CustomDialog create() { 
   LayoutInflater inflater = (LayoutInflater) context 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
   // instantiate the dialog with the custom Theme 
   final 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 button 
   if (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 GONE 
    layout.findViewById(R.id.confirm_btn).setVisibility( 
      View.GONE); 
   } 
   // set the cancel button 
   if (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 GONE 
    layout.findViewById(R.id.cancel_btn).setVisibility( 
      View.GONE); 
   } 
   // set the content message 
   if (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; 
  } 
 
 } 
}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI