gpt4 book ai didi

java - 如何从其他布局 Android 中 getText()

转载 作者:太空宇宙 更新时间:2023-11-04 12:56:41 24 4
gpt4 key购买 nike

如何从其他 .xml 中 getText() AutoCompleteTextView ?我已经尝试过这段代码:

    //getFragmentOrder
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FormDataOrder = inflater.inflate(R.layout.formdata_order_content_main, null);

ti_namapic = (AutoCompleteTextView) FormDataOrder.findViewById(R.id.ti_namapic);

String a = ti_namapic.getText().toString();
System.out.println("Value --> "+ a);

如果我在.xml中手动设置(android:text = "MyText"),我收到该文本,但如果我从设备屏幕键入/输入,我不会收到该值。

编辑:这是我的完整代码。

public class FormDataObject extends Fragment {

AutoCompleteTextView ti_desc;
AutoCompleteTextView ti_group;

View v;
Button btn_ok;

/*--------------------------------------*/
View FormDataOrder;

AutoCompleteTextView ti_namapic;

/* =========================================================================================== */
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
v = inflater.inflate(R.layout.formdata_object_content_main,container,false);


addButtonListener();

return v;
}

/* =========================================================================================== */
public void addButtonListener()
{

ti_desc = (AutoCompleteTextView) v.findViewById(R.id.ti_desc);
ti_group = (AutoCompleteTextView) v.findViewById(R.id.ti_group);

btn_ok = (Button) v.findViewById(R.id.btn_ok);
btn_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getValueFromFragmentOrder();

System.out.println("ti_desc ----> " + ti_desc.getText());
System.out.println("ti_group ----> " + ti_group.getText());
}
});
}

/* =========================================================================================== */
public void getValueFromFragmentOrder()
{
System.out.println("getComponentFromOthersFragment ----------> RUN");

//getFragmentOrder
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FormDataOrder = inflater.inflate(R.layout.formdata_order_content_main, null);

ti_namapic = (AutoCompleteTextView) FormDataOrder.findViewById(R.id.ti_namapic);

String a = ti_namapic.getText().toString();

System.out.println("Value ----------> "+ a);
}
}

“getValueFromFragmentOrder();”不工作,值 a --> 返回 null/空

最佳答案

您正在膨胀布局,但您没有在任何地方添加该 View 。首先添加 View ,然后获取值。它会起作用的。

关于java - 如何从其他布局 Android 中 getText(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35334784/

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