从片段获取edittext值 [英] Get edittext value from fragment

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

问题描述

我正在使用片段,片段中有一个edittext,我想在主要活动中获得价值.

I am using fragments,I have an edittext in fragment and I want to get value in main activity.

这是我的片段布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#878787" >

        <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="dfgdfgdf"
        android:textSize="20dp"
        android:layout_centerInParent="true"
        android:id="@+id/user_name"/>

    <EditText
    android:id="@+id/message"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
   />

    <Button 
        android:text="Gönder"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="getFromUser"
        android:layout_marginTop="40dp"
        />

</RelativeLayout>

我正在使用此功能加载片段:

I am loading fragment with this function:

public void startChat(JsonObject user) {
    FrameLayout layout = (FrameLayout)findViewById(R.id.container);
    layout.setVisibility(View.VISIBLE); 
    Bundle bundle = new Bundle();
    bundle.putString("name", user.get("name").getAsString());
    sendTo=user.get("username").getAsString();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    ConversationFragment conv = new ConversationFragment();
    conv.setArguments(bundle);
    fragmentTransaction.add(R.id.container, conv);
    fragmentTransaction.commit();
    viewPager.setVisibility(View.GONE);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayHomeAsUpEnabled(true);

}

这是我的片段类

public class ConversationFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        String name = getArguments().getString("name");
        View rootView = inflater.inflate(R.layout.fragment_conversation, container, false);
        TextView username=(TextView)rootView.findViewById(R.id.user_name);
        username.setText(name);
        return rootView;
    }
}

如您所见,当按下按钮时,主活动运行"getFromUser"功能.我想在此功能中获取edittext值.我该怎么做?

As you can see when press the button main activity runs "getFromUser" function.I want to get edittext value in this function.How can I do this ?

推荐答案

对于这些事情,它始终是相同的过程.您不能像那样访问片段的视图.您需要一个回调方法.

It's always the same procedure for these things. You can't access a fragment's views just like that. You need a callback method.

将此代码添加到 ConversationFragment :

private OnGetFromUserClickListener mListener;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (OnGetFromUserClickListener ) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnGetFromUserClickListener");
    }
}

public void getFromUser(View v) {
    if (mListener != null) {
        EditText edit = (EditText)findViewById(R.id.message);
        mListener.getFromUser(edit.getText().toString());
    }
}

public interface OnGetFromUserClickListener {
    void getFromUser(String message);
}

让您的MainActivity实现此接口.将MainFromity内部的getFromUser() 替换为:

Make your MainActivity implement this interface. Replace getFromUser() inside MainActivity with:

public void getFromUser(String message) {
    sendMessage(message);
}

完成.

实际上,当前使用XML-onClick属性是有问题的(请参见 onClick内部调用了Activity的片段):它链接到活动而不是片段.您必须以编程方式设置点击侦听器,以确保代码在将来的某个时刻不会中断.因此,为按钮提供XML内部的ID(例如get_from_user),并将此代码添加到ConversationFragment内部的 onCreateView :

Actually, using the XML-onClick attribute is currently bugged (see onClick inside fragment called on Activity): It links to the activity instead of the fragment. You have to set the click listener programmatically to make sure the code won't break at some point in the future. So give the button an ID inside the XML (e.g. get_from_user) and add this code to onCreateView inside ConversationFragment:

v.findViewById(R.id.get_from_user).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.get_from_user) {
            getFromUser(v);
        }
    }
});

使用此代码可以极大地将活动与片段彼此分离.

Using this code vastly decouples the activity and the fragment from each other.

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

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