【发布时间】:2014-08-06 14:58:20
【问题描述】:
我正在使用片段,片段中有一个编辑文本,我想在主要活动中获得价值。
这是我的片段布局
<?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>
我正在使用此功能加载片段:
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 值。我该怎么做?
【问题讨论】:
-
“主要活动运行“getFromUser”函数”如果你说那是你的片段布局没有任何意义。你的意思是 getFromUser 在片段内部被调用?
-
@Vlad 是的,我的意思是当用户按下按钮时,getFromUser 函数运行,并且这个函数在 mainactivity 中声明。
-
这正是我想的行不通的。该方法永远不会在按钮单击时调用...
-
你能发一下你的
mainactivity -
这里完整的主要活动:pastebin.com/WQHgeCdZ