Android編程實(shí)現(xiàn)在自定義對(duì)話框中獲取EditText中數(shù)據(jù)的方法
本文實(shí)例講述了Android編程實(shí)現(xiàn)在自定義對(duì)話框中獲取EditText中數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
在項(xiàng)目中忽然遇到這樣的問題,需要自定義對(duì)話框,對(duì)話框需要有一個(gè)輸入框,以便修改所選中的價(jià)格,然后點(diǎn)擊確定之后,修改所顯示的價(jià)格。遇到的最大的問題就是如何能夠獲取到自定義對(duì)話框當(dāng)中edittext輸入的數(shù)值,百度了很久,看到的答案都是如下:
//得到自定義對(duì)話框 final View DialogView = a .inflate ( R.layout.loand, null);
這是關(guān)鍵的代碼
//創(chuàng)建對(duì)話框
AlertDialog dlg = new AlertDialog.Builder(loand.this)
.setTitle("登錄框")
.setView(DialogView)//設(shè)置自定義對(duì)話框的樣式
.setPositiveButton("登陸", //設(shè)置"確定"按鈕
new DialogInterface.OnClickListener() //設(shè)置事件監(jiān)聽
{
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();
//輸入完成后,點(diǎn)擊“確定”開始登陸
c_log judge = new c_log();
boolean b_judge = judge.aa(id,password);
if(b_judge){
bar();
}else{
//加?xùn)|西
DisplayToast("NO");
}
}
})
上述方法對(duì)于使用系統(tǒng)自帶的alertdialog來(lái)說,的確是沒有問題,能夠取到你輸入的edittext的值,但對(duì)于自定義的alertdialog來(lái)說,就會(huì)始終拿到的是空的,我的解決方案是在自定義alertdialog里面取到edittext并且實(shí)例化,避免在activity里面進(jìn)行初始化,步驟如下:
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;
/**
* 系統(tǒng)設(shè)置界面
*
* @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;
// 三個(gè)自定義輸入金額
private TextView inputamount2;
private TextView inputamount5;
private TextView inputamount10;
// 跳轉(zhuǎn)到下一個(gè)頁(yè)面的4個(gè)textview,貨幣對(duì)選擇、密碼修改、憑證密碼修改、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);
// 價(jià)格修改彈出對(duì)話框
layout = inflater.inflate(
R.layout.activity_systemsettings_pricecustom_dialog,
(ViewGroup) vi.findViewById(R.id.dialog));
//。。。。。。。。。。省略了部分代碼,因?yàn)槭枪卷?xiàng)目的。。。。。。。。。。。。。。
// 三個(gè)自定義金額的監(jiān)聽事件
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;
}
});
}
// 點(diǎn)擊價(jià)格,彈出編輯框,對(duì)價(jià)格進(jìn)行修改
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("價(jià)格修改");
builder.setPositiveButton(R.string.confirm,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 獲取edittext的值
String priceStr = builder.getPrice();//關(guān)鍵代碼,getPrice放在自定義的alertdialog類里面,進(jìn)行初始化
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;
/**
* 系統(tǒng)設(shè)定中修改價(jià)格的彈出對(duì)話窗口
*
* @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;
}
//關(guān)鍵代碼
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);
//對(duì)edittext進(jìn)行初始化,關(guān)鍵代碼
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.剩下的就是布局代碼,包括一些圓角的,如果需要的可以參考我之前的博文,就不貼代碼了。完成效果如下:

總結(jié)一些,對(duì)于自定義的對(duì)話框,無(wú)法在主activity中初始化對(duì)話框里的控件的時(shí)候,可以將初始化或者取值的操作放到自定義控件里面,這樣就可以取值和賦值操作,忙活了一天,終于在師傅的指導(dǎo)下完成了這部分功能,看來(lái)單純的看網(wǎng)上的答案,可能我?guī)滋於冀鉀Q不了,還得多了解一些本質(zhì),對(duì)象得傳遞,得好好惡補(bǔ)一下基礎(chǔ)知識(shí),不懂得或者需要解釋的可以聯(lián)系我。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android中自定義對(duì)話框(Dialog)的實(shí)例代碼
- Android使用setCustomTitle()方法自定義對(duì)話框標(biāo)題
- ANDROID中自定義對(duì)話框AlertDialog使用示例
- Android 自定義對(duì)話框 showSetPwdDialog
- Android常用的AlertDialog對(duì)話框及自定義對(duì)話框
- Android自定義對(duì)話框Dialog的簡(jiǎn)單實(shí)現(xiàn)
- Android 根據(jù)EditText搜索框ListView動(dòng)態(tài)顯示數(shù)據(jù)
- Android控件系列之EditText使用方法
- Android中實(shí)現(xiàn)EditText圓角的方法
- Android EditText常用屬性功能匯總
- Android EditText自定義樣式的方法
相關(guān)文章
Android 模擬器(emulator-5554...)出現(xiàn)錯(cuò)誤解決辦法
這篇文章主要介紹了Android 模擬器出現(xiàn)錯(cuò)誤解決辦法的相關(guān)資料,如:Unable to get view server version from device,F(xiàn)ailed to install helloworld.apk on device 'emulator-5554': timeout,這種常見錯(cuò)誤,解決辦法,需要的朋友可以參考下2016-11-11
Android實(shí)現(xiàn)登錄注冊(cè)功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)登錄注冊(cè)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
AndroidStudio利用android-support-multidex解決64k的各種異常
這篇文章主要為大家詳細(xì)介紹了AndroidStudio利用android-support-multidex解決64k的各種異常,感興趣的小伙伴們可以參考一下2016-09-09
Android 勇闖高階性能優(yōu)化之啟動(dòng)優(yōu)化篇
在移動(dòng)端程序中,用戶希望的是應(yīng)用能夠快速打開。啟動(dòng)時(shí)間過長(zhǎng)的應(yīng)用不能滿足這個(gè)期望,并且可能會(huì)令用戶失望。輕則鄙視你,重則直接卸載你的應(yīng)用2021-10-10
Flutter?StreamBuilder實(shí)現(xiàn)局部刷新實(shí)例詳解
這篇文章主要為大家介紹了Flutter?StreamBuilder實(shí)現(xiàn)局部刷新實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Android中仿IOS提示框的實(shí)現(xiàn)方法
下面小編就為大家分享一篇Android中仿IOS提示框的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-01-01
Android傳遞Bitmap對(duì)象在兩個(gè)Activity之間
這篇文章主要介紹了Android傳遞Bitmap對(duì)象在兩個(gè)Activity之間的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android實(shí)現(xiàn)動(dòng)態(tài)向Gallery中添加圖片及倒影與3D效果示例
這篇文章主要介紹了Android實(shí)現(xiàn)動(dòng)態(tài)向Gallery中添加圖片及倒影與3D效果的方法,涉及Android針對(duì)圖片的加載、顯示、翻轉(zhuǎn)、倒影等相關(guān)特效功能實(shí)現(xiàn)技巧2016-08-08
Android LayoutInflater加載布局詳解及實(shí)例代碼
這篇文章主要介紹了Android LayoutInflater加載布局詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02

