功能介绍:通过一个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("\n");

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);

}

Android中fragment之间和Activity的传值、切换的更多相关文章

  1. Android中Fragment与Activity之间的交互(两种实现方式)

    (未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...

  2. Android中Fragment和ViewPager那点事儿(仿微信APP)

    在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...

  3. 深入分析:Android中app之间的交互(二,使用ComponentName)

    在前一篇相关主题的博文中我们了解了如何使用Action来启动当前应用之外的Activity处理我们的业务逻辑,在本篇笔记中我在简单介绍一下使用ComponentName来与当前应用之外的应用进行交互. ...

  4. Android 中Fragment使用

    Android 中Fragment使用 public class MainActivity extends Activity { public static String[] array = { &q ...

  5. Android中退出多个Activity的两个经典方法

    这里介绍两种方法:一种把每个activity记住,然后逐一干掉:另一种思路是使用广播. 方法一.用list保存activity实例,然后逐一干掉 上代码: import java.util.Linke ...

  6. Android中的this、Activity、Context等

    Android中的this.Activity.Context.Application等虽然有相似之处,但是不能乱用,每一个都有自己的特点.用的时候不能太随意了. 避免context相关的内存泄露,注意 ...

  7. 【Android】安卓开发之activity如何传值到fragment,activity与fragment传值

    作者:程序员小冰,GitHub主页:https://github.com/QQ986945193 新浪微博:http://weibo.com/mcxiaobing 大家知道,我们利用activity使 ...

  8. android中使用Intent在activity之间传递数据

    android中intent传递数据的简单使用: 1.使用intent传递数据: 首先将需要传递的数据放入到intent中 Intent intent = new Intent(MainActivit ...

  9. Android中Fragment的两种创建方式

    fragment是Activity中用户界面的一个行为或者是一部分.你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用.你可以认 ...

随机推荐

  1. Ext.require 的作用(转)

    Ext.require:用到哪些组件,然后就预先加载,多余不用加载的组件 在实际环境中我们都会用 ext-all.js, 但是在开发调试的时候,我们使用 require 的话它可以动态加载单个的 js ...

  2. myeclipse2014安装aptana3.4.0插件(转)

    1.下载aptana3.4.0_eclipse的zip包  http://pan.baidu.com/s/1qXOiZl6 或者是:https://pan.baidu.com/s/1jIqOYcI 2 ...

  3. 利用反射绑定事件处理程序(C#)

    利用反射绑定事件处理程序(C#) 传统的写法:强类型的情况下 using System;using System.Collections.Generic;using System.Text; usin ...

  4. as3.0复制影片简介(自我复制的三种形式)

    //mc是被复制影片简介的实例名,(===在库中找到mc影片简介,右击“属性”,点击“为actionscript导出”,选中确定即可===这个是重点) var newSprite:Sprite=mc; ...

  5. NumPy 位运算

    NumPy 位运算 NumPy "bitwise_" 开头的函数是位运算函数. NumPy 位运算包括以下几个函数: 函数 描述 bitwise_and 对数组元素执行位与操作 b ...

  6. Linux之间配置SSH互信(SSH免密码登录)

    为简化SSH过程,采用证书方式,免去SSH登入时需要输入账号密码的过程,具体操作如下: 一.在SSH服务器所在机器上 1.以root用户登录,更改ssh配置文件 /etc/ssh/sshd_confi ...

  7. TZOJ 4325 RMQ with Shifts(线段树查询最小,暴力更新)

    描述 In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each que ...

  8. TZOJ 2999 Network(连通图割点数量)

    描述 A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting ...

  9. 【校招面试 之 C/C++】第33题 C++ 11新特性(四)之STL容器

    C++ 11新增array.forward_list(单链表).unordered_set.unordered_map集中容器.

  10. day 21 封装,多态,类的其他属性

    封装 封装:将一些数据,重要的信息等等放到一个地方(空间中) class A: country = 'China' area = '深圳' def __init__(self,name,age): s ...