Android中fragment之间和Activity的传值、切换
介紹:
?
功能介紹:通過一個activity下方的三個按鈕,分別是發(fā)送消息(sendButton)、聊天記錄(chatButton)、常用語(commonButton)。當單擊按鈕是,來切換上方的fragment,用以顯示不同的內(nèi)容。
?
所用的知識點:當單擊發(fā)送消息按鈕時:
?
1.從MainActivity中把EditText中的值傳到fragment中。
2.fragment如何動態(tài)的顯示在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中的內(nèi)容
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,防止獲取不到對應(yīng)fragment中控件
//將要加載到MainActivity中fragment實例化對象
fragment_sendContent f1 = new fragment_sendContent();
//第一個參數(shù)是main_activity.xml中的FrameLayout的對象
transaction.add(R.id.frameLayout_content, f1);
//加入到后臺棧中
transaction.addToBackStack(null);
//事務(wù)必須提交
transaction.commit();
?
?
?
?
當單擊聊天記錄按鈕時:現(xiàn)象:獲取到前面所有發(fā)送的內(nèi)容。
?
所用到的知識點:
1.如何獲取MainActivity中的數(shù)據(jù)(剛才發(fā)送的數(shù)據(jù)放在MainActivity中的數(shù)組中)
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
//加載對應(yīng)的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中的數(shù)據(jù)
String chatString = getActivity().getIntent().getStringExtra("chatStr");
?
textView.setText(chatString);
?
return contentView;
?
}
?
針對第二個問題:
?
fragment的動態(tài)切換:
?
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();
}
};
?
?
?
?
當單擊常用語按鈕時:現(xiàn)象:彈出經(jīng)常使用的用語(實現(xiàn)最麻煩)。
?
?
所用到的知識點:
1.利用ListView控件顯示常用語
2.如何將數(shù)據(jù)放置到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);
//第一個參數(shù):
//第二個參數(shù):是一個List<E>參數(shù)
//第三個參數(shù):另一個布局文件,其中用一個textView控件來顯示每一條listview中信息
//第四個參數(shù):
//第五個參數(shù):
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是觸發(fā)的事件
public class listener{
public OnItemClickListener itemListener = new OnItemClickListener() {
?
@Override
//第三個參數(shù)表示的是給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);
}
?
};
?
//上面的單擊事件只能發(fā)生在onResume()中,不能放在onCreatView()中
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
?
listView.setOnItemClickListener(new listener().itemListener);
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/BeyondAverage0908/p/3717619.html
總結(jié)
以上是生活随笔為你收集整理的Android中fragment之间和Activity的传值、切换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oppo怎么拦截广告电话(这几个品牌是什
- 下一篇: python调用c代码