如何从片段获取EditText [英] How to get EditText from fragment

查看:46
本文介绍了如何从片段获取EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了AlerDialog:

I created the AlerDialog:

AlertDialog.Builder alert = new AlertDialog.Builder(appContext);

                alert.setTitle("Add subcontractors").setView(R.layout.add_subcontractor_form);
                
                //final EditText input = new EditText(appContext);

                alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        String YouEditTextValue = input.getText().toString();
                    }
                });

                alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // what ever you want to do with No option.
                    }
                });

                alert.show();

并为其设置布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
           <EditText
            android:layout_margin="10dp"
            android:id="@+id/et_sub_name"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Type name here"
            android:textSize="20sp"/>
</LinearLayout>

我的问题是,如何从代码中的布局中获取EditText视图?因为我想在用户按下确定"按钮后输入文本.按钮.

My question is, how to get EditText view from my layout in code? Because I want to have an inputted text after the user pressed the "OK" button.

推荐答案

尝试执行以下操作:

LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.add_subcontractor_form, null);
alert.setView(dialogView);

EditText editText = (EditText) dialogView.findViewById(R.id.et_sub_name);

这篇关于如何从片段获取EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆