zoukankan      html  css  js  c++  java
  • 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.熟悉SimpleAdapterMap<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

    //第三个参数表示的是给itemlistView的位置

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

    }

  • 相关阅读:
    每天一道LeetCode--141.Linked List Cycle(链表环问题)
    每天一道LeetCode--119.Pascal's Triangle II(杨辉三角)
    每天一道LeetCode--118. Pascal's Triangle(杨辉三角)
    CF1277D Let's Play the Words?
    CF1281B Azamon Web Services
    CF1197D Yet Another Subarray Problem
    CF1237D Balanced Playlist
    CF1239A Ivan the Fool and the Probability Theory
    CF1223D Sequence Sorting
    CF1228D Complete Tripartite
  • 原文地址:https://www.cnblogs.com/BeyondAverage0908/p/3717619.html
Copyright © 2011-2022 走看看