日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發(fā)布時間:2023/12/19 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android中fragment之间和Activity的传值、切换 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

介紹:

?

功能介紹:通過一個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,防止獲取不到對應fragment中控件

//將要加載到MainActivity中fragment實例化對象

fragment_sendContent f1 = new fragment_sendContent();

//第一個參數(shù)是main_activity.xml中的FrameLayout的對象

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

//加入到后臺棧中

transaction.addToBackStack(null);

//事務必須提交

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

//加載對應的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.熟悉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);

//第一個參數(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ù)表示的是給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);

}

?

};

?

//上面的單擊事件只能發(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。