我将在Dialogs上浏览Google的 Android Developer页面,特别是 this部分.但是,我没有以编程方式创建DialogFragment的消息,而是使用以下元素创建了一个名为layout_newpayperiod.xml的预设布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Spinner
    android:id="@+id/spinner_payperiod"
    android:layout_width="fill_parent"
    android:layout_height="48dp"
    android:padding="8dp"
    android:entries="@array/pay_periods"
    />

<EditText
    android:id="@+id/edittext_savepercent"
    android:layout_width="fill_parent"
    android:layout_height="48dp"
    android:padding="8dp"
    android:inputType="number"
    android:hint="Percent to Save"
    />

<EditText
    android:id="@+id/edittext_payment"
    android:layout_width="fill_parent"
    android:layout_height="48dp"
    android:padding="8dp"
    android:inputType="numberDecimal"
    android:hint="Total Payment"
    />

</LinearLayout>

当我调用DialogFragment时,它会正常显示,Spinner具有正确的值.我填写了条目并点击“确定”,但是当我尝试从Spinner和两个EditText字段中检索值时,应用程序强制关闭NumberFormatException:无效的双“”.我觉得我没有正确地检索视图.谁能帮我解决这个问题呢?谢谢!

public class StartPayperiodDialogFragment extends DialogFragment {

/* The activity that creates an instance of this dialog fragment must
 * implement this interface in order to receive event callbacks.
 * Each method passees the DialogFragment in case the host needs to query it.
 */
public interface StartPayperiodDialogListener{
    public void onDialogPositiveClick(DialogFragment dialog);
    public void onDialogNegativeClick(DialogFragment dialog);
}

// Use this instance of the interface to deliver action events
StartPayperiodDialogListener listener;

// Override the Fragment.onAttach() method to instantiate the StartPayperiodDialogListener
@Override
public void onAttach(Activity activity){
    super.onAttach(activity);
    // Verify that the host activity implements the callback interface
    try{
        // Instantiate the NoticeDialogListener so we can send events to the host
        listener = (StartPayperiodDialogListener) activity;
    }catch(ClassCastException e){
        // The activity doesn't implement the interface,throw exception
        throw new ClassCastException(activity.toString() + " must implement StartPayperiodDialogListener");
    }
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    View transactionLayout = View.inflate(getActivity(),R.layout.layout_newpayperiod,null);
    builder.setView(transactionLayout)
    .setPositiveButton("OK",new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog,int which) {
            // Send the positive button event back to the calling activity
            listener.onDialogPositiveClick(StartPayperiodDialogFragment.this);
        }
    })
    .setNegativeButton("Cancel",int which) {
            // Send the negative button event back to the calling activity
            listener.onDialogNegativeClick(StartPayperiodDialogFragment.this);
        }
    });
    return builder.create();
}

}

在MainActivity.class中,回调方法:

@Override
public void onDialogPositiveClick(DialogFragment dialog) {
    // User pressed OK,so we need to grab the values from the
    // dialog's fields and apply them to the Views in the Main
    // Activity

    View transactionLayout = View.inflate(this,null);

    // Start with the payment amount
    EditText paymentEt = (EditText) transactionLayout.findViewById(R.id.edittext_payment);
    TextView paymentTv = (TextView) findViewById(R.id.text_paycheck);
    paymentTv.setText(moneyFormat.format(Double.parseDouble(paymentEt.getText().toString())));

    // Next,the percent to save
    EditText savingsEt = (EditText) transactionLayout.findViewById(R.id.edittext_savepercent);
    TextView savingsTv = (TextView) findViewById(R.id.text_savings);
    savingsTv.setText(savingsEt.getText().toString() + "%");

    // Then,the pay period
    Spinner periodSp = (Spinner) transactionLayout.findViewById(R.id.spinner_payperiod);
    TextView periodTv = (TextView) findViewById(R.id.text_payperiod);
    periodTv.setText(periodSp.getSelectedItem().toString());

    // Finally,let's update the daily allowance amount and clear
    // the adapter
    adapter.clear();
    adapter.notifyDataSetChanged();
    TextView allowanceTv = (TextView) findViewById(R.id.text_allowance);
    Double allowanceValue;
    switch(periodSp.getSelectedItemPosition()){
        case(0):    // Daily
            allowanceValue = Double.parseDouble(paymentTv.getText().toString());
            break;
        case(1):    // Weekly
            allowanceValue = Double.parseDouble(paymentTv.getText().toString()) / 7;
            break;
        case(2):    // 2 Weeks
            allowanceValue = Double.parseDouble(paymentTv.getText().toString()) / 14;
            break;
        case(3):    // 30 Days
            allowanceValue = Double.parseDouble(paymentTv.getText().toString()) / 30;
            break;
        default:    // Debugging purposes only
            allowanceValue = 42.0;
            break;
    }
    allowanceTv.setText(Double.toString(allowanceValue));
}

解决方法

试试这个:
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
    // User pressed OK,so we need to grab the values from the
    // dialog's fields and apply them to the Views in the Main
    // Activity

    // Start with the payment amount
    Dialog dialogView = dialog.getDialog();
    EditText paymentEt = (EditText) dialogView.findViewById(R.id.edittext_payment);

…等等(通过以相同的方式查询dialogView,从对话框中检索任何其他视图.)

您的膨胀代码“膨胀”该视图的全新版本.您想要访问在对话框中创建的那个.

android – 从DialogFragment中的EditText中检索值的更多相关文章

  1. HTML5实现直播间评论滚动效果的代码

    这篇文章主要介绍了HTML5实现直播间评论滚动效果的代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  2. 前端监听websocket消息并实时弹出(实例代码)

    这篇文章主要介绍了前端监听websocket消息并实时弹出,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  3. HTML5之消息通知的使用(Web Notification)

    通知可以说是web中比较常见且重要的功能,私信、在线提问、或者一些在线即时通讯工具我们总是希望第一时间知道对方有了新的反馈。本篇文章主要介绍了HTML5之消息通知的使用(Web Notification),感兴趣的小伙伴们可以参考一下

  4. HTML5中的Web Notification桌面通知功能的实现方法

    这篇文章主要介绍了HTML5中的Web Notification桌面通知功能的实现方法,需要的朋友可以参考下

  5. HTML5仿微信聊天界面、微信朋友圈实例代码

    小编最近开发一个基于html5开发的一个微信聊天前端界面,功能很全面,下面小编给大家分享实例代码,需要的朋友参考下

  6. HTML5的postMessage的使用手册

    HTML5提出了一个新的用来跨域传值的方法,即postMessage,这篇文章主要介绍了HTML5的postMessage的使用手册的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  7. ios – Testflight无法安装应用程序

    我有几个测试人员注册了testflight并连接了他们的设备……他们有不同的ios型号……但是所有这些都有同样的问题.当他们从“safari”或“testflight”应用程序本身单击应用程序的安装按钮时……达到约90%并出现错误消息…

  8. xcode找不到匹配的配置文件

    我有一个AdhociOS应用程序,它给了我“在xcode6中找不到匹配的配置文件”,我创建了一个Adhoc配置文件,下载它,双击它并在General–Identity下选择了一个团队.但我接着得到了那条消息,并尝试使用“修复问题”按钮没有帮助.在构建设置–供应配置文件–发布我有“自动”.任何人都可以帮助我,我完全迷失了……

  9. ios – Reactive Cocoa – 以编程方式设置文本时不会调用UITextView的rac_textSignal

    我正在实现一个聊天UI,并使用ReactiveCocoa根据用户的类型调整聊天气泡的大小.目前,我正在基于textview的rac_textSignal更新UI的布局.一切都很好–除了一点:当用户发送消息时,我以编程方式清除文本字段:…我是否需要拥有一个持有currentTypedString的Nsstring,并在该字符串更新时驱动UI更改?

  10. ios – 当我关闭应用程序时,我从调试器获得消息:由于信号15而终止

    我怎么能解决这个问题,我不知道这个链接MypreviousproblemaboutCoredata对我的问题有影响吗?当我cmd应用程序的Q时,将出现此消息.Messagefromdebugger:Terminatedduetosignal15如果谁知道我以前的问题的解决方案,请告诉我.解决方法>来自调试器的消息:每当用户通过CMD-Q(退出)或STOP手动终止应用程序(无论是在iOS模拟器中还是

随机推荐

  1. bluetooth-lowenergy – Altbeacon库无法在Android 5.0上运行

    昨天我在Nexus4上获得了Android5.0的更新,并且altbeacon库停止了检测信标.似乎在监视和测距时,didEnterRegion和didRangeBeaconsInRegion都没有被调用.即使RadiusNetworks的Locate应用程序现在表现不同,一旦检测到信标的值,它们就不再得到更新,并且通常看起来好像信标超出了范围.我注意到的一点是,现在在logcat中出现以下行“B

  2. android – react-native动态更改响应者

    我正在使用react-native进行Android开发.我有一个视图,如果用户长按,我想显示一个可以拖动的动画视图.我可以使用PanResponder实现这一点,它工作正常.但我想要做的是当用户长按时,用户应该能够继续相同的触摸/按下并拖动新显示的Animated.View.如果您熟悉Google云端硬盘应用,则它具有类似的功能.当用户长按列表中的任何项目时,它会显示可拖动的项目.用户可以直接拖

  3. android – 是否有可能通过使用与最初使用的证书不同的证书对其进行签名来发布更新的应用程序

    是否可以通过使用与最初使用的证书不同的证书进行签名来发布Android应用程序的更新?我知道当我们尝试将这样的构建上传到市场时,它通常会给出错误消息.但有没有任何出路,比如将其标记为主要版本,指定市场中的某个地方?解决方法不,你不能这样做.证书是一种工具,可确保您是首次上传应用程序的人.所以总是备份密钥库!

  4. 如何检测Android中是否存在麦克风?

    ..所以我想在让用户访问语音输入功能之前检测麦克风是否存在.如何检测设备上是否有麦克风.谢谢.解决方法AndroidAPI参考:hasSystemFeature

  5. Android – 调用GONE然后VISIBLE使视图显示在错误的位置

    我有两个视图,A和B,视图A在视图B上方.当我以编程方式将视图A设置为GONE时,它将消失,并且它正下方的视图将转到视图A的位置.但是,当我再次将相同的视图设置为VISIBLE时,它会在视图B上显示.我不希望这样.我希望视图B回到原来的位置,这是我认为会发生的事情.我怎样才能做到这一点?编辑–代码}这里是XML:解决方法您可以尝试将两个视图放在RelativeLayout中并相对于彼此设置它们的位置.

  6. android – 获得一首歌的流派

    我如何阅读与歌曲相关的流派?我可以读这首歌,但是如何抓住这首歌的流派,它存放在哪里?解决方法检查此代码:

  7. android – 使用textShadow折叠工具栏

    我有一个折叠工具栏的问题,在展开状态我想在文本下面有一个模糊的阴影,我使用这段代码:用:我可以更改textColor,它可以工作,但阴影不起作用.我为阴影尝试了很多不同的值.是否可以为折叠文本投射阴影?

  8. android – 重用arm共享库

    我已经建立了armarm共享库.我有兴趣重用一个函数.我想调用该函数并获得返回值.有可能做这样的事吗?我没有任何头文件.我试过这个Android.mk,我把libtest.so放在/jni和/libs/armeabi,/lib/armeabi中.此时我的cpp文件编译,但现在是什么?我从objdump知道它的名字编辑:我试图用这个android.mk从hello-jni示例中添加prebuild库:它工作,但libtest.so相同的代码显示以下错误(启动时)libtest.so存在于libhello-j

  9. android – 为NumberPicker捕获键盘’Done’

    我有一个AlertDialog只有一些文本,一个NumberPicker,一个OK和一个取消.(我知道,这个对话框还没有做它应该保留暂停和恢复状态的事情.)我想在软键盘或其他IME上执行“完成”操作来关闭对话框,就像按下了“OK”一样,因为只有一个小部件可以编辑.看起来处理IME“Done”的最佳方法通常是在TextView上使用setonEditorActionListener.但我没有任何Te

  10. android – 想要在调用WebChromeClient#onCreateWindow时知道目标URL

    当我点击一个带有target=“_blank”属性的超链接时,会调用WebChromeClient#onCreateWindow,但我找不到新的窗口将打开的新方法?主页url是我唯一能知道的东西?我想根据目标网址更改应用行为.任何帮助表示赞赏,谢谢!

返回
顶部