gpt4 book ai didi

android - 如何从 fragment 中获取 EditText

转载 作者:搜寻专家 更新时间:2023-11-01 07:49:45 25 4
gpt4 key购买 nike

我创建了 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 View ?因为我想在用户按下“确定”按钮后输入文本。

最佳答案

尝试这样做:

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);

关于android - 如何从 fragment 中获取 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36604318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com