Android中fragment之间和Activity的传值、切换

介绍:

功能介绍:通过一个 activity 下方的三个按钮,分别是发送消息( sendButton )、聊天记录( chatButton )、常用语 (commonButton) 。当单击按钮是,来切换上方的 fragment ,用以显示不同的内容。

所用的知识点:当单击发送消息按钮时:

1. 从 MainActivity 中把 EditText 中的值传到 fragment 中。

2. fragment 如何动态的显示在MainActivity中。  

针对第一个问题:在 sendButton 单击事件中:

private OnClickListener sendListener = new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

// 获取 fragment1 中的 text 控件 可以在 MainActivity 中直接获取 fragment 中的控件

TextView showView=(TextView)findViewById(R.id.showtextviewId);

// 得到 EditText 中的内容

String message = editText.getText().toString();

//getIntent().putExtra("str", message);

//String temp = getIntent().getStringExtra("str");

str = str.append(message).append(" ");

String text=null;

if(str.length()>100){

text = str.substring(str.length()-100, str.length()-1).toString();

}else {

text = str.toString();

}

// 将获取的值放置到 intent 中,方便以后再 fragment 中获取

getIntent().putExtra("chatStr", str.toString());

showView.setText(text);

}

};

针对第二个问题:

// 申明 FragmentManager 对象,和 FragmentTransaction

private FragmentManager manager;

private FragmentTransaction transaction;

// 获取两个对象的方法

manager = getSupportFragmentManager();

transaction = manager.beginTransaction();

// 必须先初始化一个 fragment ,防止获取不到对应 fragment 中控件

// 将要加载到MainActivity中 fragment 实例化对象

fragment_sendContent f1 = new fragment_sendContent();

// 第一个参数是 main_activity.xml 中的 FrameLayout 的对象

transaction.add(R.id.frameLayout_content, f1);

// 加入到后台栈中

transaction.addToBackStack(null);

// 事务必须提交

transaction.commit();

当单击聊天记录按钮时:现象:获取到前面所有发送的内容。

所用到的知识点:

1. 如何获取MainActivity中的数据(刚才发送的数据放在MainActivity中的数组中)

2. 从一个 fragment 切换到另一个 fragment 中。

针对第一个问题:

先看在 sendListener 事件中:

// 将获取的值放置到 intent 中,方便以后再 fragment 中获取

getIntent().putExtra("chatStr", str.toString());

在历史记录的 fragment.java 中:

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

// TODO Auto-generated method stub

// 加载对应的 fragment ,这里是 history_record

View contentView = inflater.inflate(R.layout.history_record, null);

contentView.setBackgroundColor(Color.MAGENTA);

// 获取这个 fragment 中的控件的方法

textView = (TextView) contentView.findViewById(R.id.recordTextViewId);

// 获取MainActivity中的数据

String chatString = getActivity().getIntent().getStringExtra("chatStr");

textView.setText(chatString);

return contentView;

}

针对第二个问题:

fragment 的动态切换:

private OnClickListener chatListener = new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

fragment_HistoryRecord historyRecord = new fragment_HistoryRecord();

transaction = manager.beginTransaction();

transaction.replace(R.id.frameLayout_content, historyRecord);

transaction.addToBackStack(null);

transaction.commit();

}

};

当单击常用语按钮时:现象:弹出经常使用的用语(实现最麻烦)。

所用到的知识点:

1. 利用 ListView 控件显示常用语

2. 如何将数据放置到 ListView 

3. 熟悉 SimpleAdapter 、 Map<K,V>,ArrayList<E> 之间的配合使用

4. 从一个 fragment 切换到另一个 fragment 中。(不赘述)

针对第一个问题:

新建一个 xml 文件,里面加上一个 ListView 即可。

<ListView

    android:id="@+id/commonLanguageId"

    android:layout_width="match_parent"

                android:layout_height="200dp">

</ListView>

针对第二个问题:

// 三个全局变量

private ListView listView;

private String[] strs;

private EditText editText;

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View contentView = inflater.inflate(R.layout.fragment_listview, null);

// 获取 fragment 中的控件对象

listView = (ListView) contentView.findViewById(R.id.commonLanguageId);

// 第一个参数:

// 第二个参数:是一个 List<E> 参数

// 第三个参数:另一个布局文件,其中用一个 textView 控件来显示每一条 listview 中信息

// 第四个参数:

// 第五个参数:

SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity().getApplicationContext(),

getData(), R.layout.show_item, new String[]{"common"}, new int[]{R.id.item});

listView.setAdapter(simpleAdapter);

return contentView;

}

// 将申明的几个常用语加入到 ArrayList 中,使用键值对

public ArrayList<HashMap<String, String>> getData(){

ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();

strs = new String[]{

小孩 "," 小姐 "," 小东王 "," 江西 "," 你好! "

};

for(int i=0;i<strs.length;i++){

HashMap<String, String> map = new HashMap<>();

map.put("common", strs[i]);

list.add(map);

}

return list;

}

// 当单击每一条 item 是触发的事件

public class listener{

public OnItemClickListener itemListener = new OnItemClickListener() {

@Override

// 第三个参数表示的是给 item 在 listView 的位置

public void onItemClick(AdapterView<?> arg0, View arg1, int position ,

long arg3) {

// TODO Auto-generated method stub

// 获取 MainActivity 中的控件对象

editText = (EditText) getActivity().findViewById(R.id.editTextId);

String string  = strs[position];

editText.setText(string);

}

};

// 上面的单击事件只能发生在 onResume() 中,不能放在 onCreatView() 

@Override

public void onResume() {

// TODO Auto-generated method stub

super.onResume();

listView.setOnItemClickListener(new listener().itemListener);

}

原文地址:https://www.cnblogs.com/a354823200/p/4342039.html