如何从其他布局Android获取文本()

时间:2016-02-11 09:00:04

标签: java android fragment

如何从其他.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 - > return null / empty

3 个答案:

答案 0 :(得分:0)

您正在对布局进行充气,但您并未在任何地方添加该视图。 首先添加视图,然后获取值。它会起作用。

答案 1 :(得分:0)

您实际需要做的是获取onClick btn_ok事件或您需要时的文字。您现在正在做的是在调用findViewById方法后立即检索该值。

这里发生了什么?

您将只获得在EditText中指定的初始值,并将其存储到变量,但不会在用户输入文本后存储。

您需要做什么?

- 修改此

btn_ok.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        getValueFromFragmentOrder();
        String a = ti_namapic.getText().toString();

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

答案 2 :(得分:0)

试试这个......

 View mOrderContent;

 .....

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

    //getFragmentOrder
    LayoutInflater inflater = getActivity().getLayoutInflater();
    LinearLayout viewGroup = new LinearLayout(getActivity());
    mOrderContent = inflater.inflate(R.layout.formdata_order_content_main, viewGroup);
    AutoCompleteTextView ti_namapic = (AutoCompleteTextView) mOrderContent.findViewById(R.id.ti_namapic);

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

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