这篇文章将为大家详细讲解有关Android编程怎么实现在自定义对话框中获取EditText数据,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
在项目中忽然遇到这样的问题,需要自定义对话框,对话框需要有一个输入框,以便修改所选中的价格,然后点击确定之后,修改所显示的价格。遇到的最大的问题就是如何能够获取到自定义对话框当中edittext输入的数值,百度了很久,看到的答案都是如下:
//得到自定义对话框 final View DialogView = a .inflate ( R.layout.loand, null);
这是关键的代码
//创建对话框 AlertDialog dlg = new AlertDialog.Builder(loand.this) .setTitle("登录框") .setView(DialogView)//设置自定义对话框的样式 .setPositiveButton("登陆", //设置"确定"按钮 new DialogInterface.OnClickListener() //设置事件监听 { public void onClick(DialogInterface dialog, int whichButton) { editText1 =(EditText) DialogView.findViewById(R.id.editText1); editText2 =(EditText) DialogView.findViewById(R.id.editText2); String id = editText1.getText().toString(); String password = editText2.getText().toString(); //输入完成后,点击“确定”开始登陆 c_log judge = new c_log(); boolean b_judge = judge.aa(id,password); if(b_judge){ bar(); }else{ //加东西 DisplayToast("NO"); } } })
上述方法对于使用系统自带的alertdialog来说,的确是没有问题,能够取到你输入的edittext的值,但对于自定义的alertdialog来说,就会始终拿到的是空的,我的解决方案是在自定义alertdialog里面取到edittext并且实例化,避免在activity里面进行初始化,步骤如下:
1.主要的activity主类代码:
package client.verbank.mtp.allone.frame.systemsettings; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.TextView; import android.widget.ToggleButton; import client.verbank.mtp.allone.R; import client.verbank.mtp.allone.component.CustomDialogPriceModify; import client.verbank.mtp.allone.consts.IBundleCommData; import client.verbank.mtp.allone.consts.ISystemCommData; import client.verbank.mtp.allone.frame.ControlFragment; import client.verbank.mtp.allone.frame.MainActivity; import client.verbank.mtp.allone.frame.systemsettings.nextpage.ChangePasswordActivity; import client.verbank.mtp.allone.frame.systemsettings.nextpage.ChangePhonePinActivity; import client.verbank.mtp.allone.frame.systemsettings.nextpage.ChooseRssActivity; import client.verbank.mtp.allone.frame.systemsettings.nextpage.SelectInstrumentActivity; import client.verbank.mtp.allone.util.SharepreferencesUtilSystemSettings; /** * 系统设置界面 * * @Project: FEIB_AndroidStation * @Title: SystemSettingsFragment.java * @Package client.verbank.mtp.allone.frame.systemsettings * @Description: TODO * @author qiulinhe qiu.linhe@allone.cn * @date 2015年9月29日 上午11:48:53 * @Copyright: 2015 www.allone.cn Inc. All rights reserved. * @version V3.0.0 */ public class SystemSettingsFragment extends ControlFragment implements ISystemCommData { // 四组需要打钩的textview初始化 private TextView currencypairs; private TextView openpositionprice; private TextView floatingprofit; private TextView dealtime; private TextView ordercurrencypairs; private TextView pricehighend; private TextView priceendhigh; private TextView summarycurrencypairs; private TextView summaryfloatingprofit; private TextView riseandfall; private TextView applies; // 三个自定义输入金额 private TextView inputamount2; private TextView inputamount5; private TextView inputamount10; // 跳转到下一个页面的4个textview,货币对选择、密码修改、凭证密码修改、RSS源选择 private TextView Currencyofchoice; private TextView changepassword; private TextView changecertificatepassword; private TextView rsssource; ToggleButton mosthightoggle; ToggleButton mostlowtoggle; ToggleButton riseandfallmenutoggle; ToggleButton pricetimetoggle; View layout; // EditText price; AlertDialog dlg; int flagprice = 0; private Handler handler = new Handler(); public SystemSettingsFragment(MainActivity activity) { super(activity); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View vi = inflater.inflate(R.layout.system_settings, container, false); // 价格修改弹出对话框 layout = inflater.inflate( R.layout.activity_systemsettings_pricecustom_dialog, (ViewGroup) vi.findViewById(R.id.dialog)); //。。。。。。。。。。省略了部分代码,因为是公司项目的。。。。。。。。。。。。。。 // 三个自定义金额的监听事件 private void selfThreeMoneyLister(View vi) { inputamount2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { modifyPriceDialog(); flagprice = 1; } }); inputamount5.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { modifyPriceDialog(); flagprice = 2; } }); inputamount10.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { modifyPriceDialog(); flagprice = 3; } }); } // 点击价格,弹出编辑框,对价格进行修改 private void modifyPriceDialog() { LayoutInflater factory = LayoutInflater.from(getActivity()); final View DialogView = factory.inflate( R.layout.activity_systemsettings_pricecustom_dialog, null); final CustomDialogPriceModify.Builder builder = new CustomDialogPriceModify.Builder( getActivity()); builder.setTitle("價格修改"); builder.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 获取edittext的值 String priceStr = builder.getPrice();//关键代码,getPrice放在自定义的alertdialog类里面,进行初始化 if (flagprice == 1) { inputamount2.setText(priceStr); SharepreferencesUtilSystemSettings.putValue( getActivity(), System_key_SelfAmout2, priceStr); } else if (flagprice == 2) { inputamount5.setText(priceStr); SharepreferencesUtilSystemSettings.putValue( getActivity(), System_key_SelfAmout5, priceStr); } else { inputamount10.setText(priceStr); SharepreferencesUtilSystemSettings.putValue( getActivity(), System_key_SelfAmout10, priceStr); } // price.setText(""); /** * 取得view的父组件,然后移除view */ dialog.dismiss(); } }); builder.setNegativeButton(R.string.cancel, new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub // ((ViewGroup) layout.getParent()).removeView(layout); dialog.dismiss(); } }); builder.create().show(); } }
2.自定义alertdialog类,CustomDialogPriceModify.java,如下:
package client.verbank.mtp.allone.component; 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.EditText; import android.widget.TextView; import client.verbank.mtp.allone.R; /** * 系统设定中修改价格的弹出对话窗口 * * @author wangyubo * */ public class CustomDialogPriceModify extends Dialog { public CustomDialogPriceModify(Context context) { super(context); } public CustomDialogPriceModify(Context context, int theme) { super(context, theme); } public static class Builder { private Context context; private String title; private float textSize; private String message; private String positiveButtonText; private String negativeButtonText; private View contentView; private EditText priceText; private DialogInterface.OnClickListener positiveButtonClickListener; private DialogInterface.OnClickListener negativeButtonClickListener; public Builder(Context context) { this.context = context; } public Builder setMessage(String message) { this.message = message; return this; } public Builder setMessage(String message, float textSize) { this.message = message; this.textSize = textSize; 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; } public Builder setContentView(View v) { this.contentView = v; return this; } /** * Set the positive button resource and it's listener * * @param positiveButtonText * @return */ public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) { this.positiveButtonText = (String) context .getText(positiveButtonText); this.positiveButtonClickListener = listener; return this; } //关键代码 public String getPrice() { return priceText.getText().toString(); } public Builder setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) { this.positiveButtonText = positiveButtonText; this.positiveButtonClickListener = listener; return this; } public Builder setNegativeButton(int negativeButtonText, DialogInterface.OnClickListener listener) { this.negativeButtonText = (String) context .getText(negativeButtonText); this.negativeButtonClickListener = listener; return this; } public Builder setNegativeButton(String negativeButtonText, DialogInterface.OnClickListener listener) { this.negativeButtonText = negativeButtonText; this.negativeButtonClickListener = listener; return this; } public CustomDialogPriceModify create() { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); // instantiate the dialog with the custom Theme final CustomDialogPriceModify dialog = new CustomDialogPriceModify( context, R.style.Dialog); dialog.setCanceledOnTouchOutside(false); dialog.setCancelable(false); View layout = inflater.inflate( R.layout.activity_systemsettings_pricecustom_dialog, null); dialog.addContentView(layout, new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); // set the dialog title ((TextView) layout.findViewById(R.id.title)).setText(title); priceText = (EditText) layout.findViewById(R.id.price); //对edittext进行初始化,关键代码 if (positiveButtonText != null) { ((Button) layout.findViewById(R.id.positiveButton)) .setText(positiveButtonText); if (positiveButtonClickListener != null) { ((Button) layout.findViewById(R.id.positiveButton)) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE); } }); } } else { // if no confirm button just set the visibility to GONE layout.findViewById(R.id.positiveButton).setVisibility( View.GONE); } // set the cancel button if (negativeButtonText != null) { ((Button) layout.findViewById(R.id.negativeButton)) .setText(negativeButtonText); if (negativeButtonClickListener != null) { ((Button) layout.findViewById(R.id.negativeButton)) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE); } }); } } else { // if no confirm button just set the visibility to GONE layout.findViewById(R.id.negativeButton).setVisibility( View.GONE); } // set the content message if (message != null) { TextView msgView = (TextView) layout.findViewById(R.id.message); msgView.setText(message); if (textSize != 0) { msgView.setTextSize(textSize); } } else if (contentView != null) { } dialog.setContentView(layout); return dialog; } } }
3.剩下的就是布局代码,包括一些圆角的,如果需要的可以参考我之前的博文,就不贴代码了。完成效果如下:
Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。
关于“Android编程怎么实现在自定义对话框中获取EditText数据”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。