本文實例講述了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("
主站蜘蛛池模板:
烟台市|
武威市|
房山区|
四子王旗|
定西市|
迁安市|
岚皋县|
高淳县|
黎平县|
峨山|
宁化县|
百色市|
古蔺县|
石楼县|
遂川县|
屯昌县|
大渡口区|
宜宾县|
绵阳市|
嘉义县|
德阳市|
白银市|
恩平市|
吴桥县|
武冈市|
凤庆县|
抚宁县|
新闻|
孝义市|
绥棱县|
长丰县|
绥滨县|
远安县|
神农架林区|
阿拉善左旗|
阿图什市|
邵阳市|
海丰县|
沿河|
玉田县|
黄龙县|