上一篇文章简单介绍了
Android的回调机制的使用,这一篇博文将重点介绍Fragment碎片与activity的数据交互,fragment在Android开发中起着至关重要的作用,通过官方Android api我们可以看到,fragment有着自己的生命周期并依赖于它绑定的activity的生命周期而存在,那样activity与fragment怎样进行数据交互呢,请看此篇博文。
Fragment02的代码
main的xml
fragment的xml
MainActiivity的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
package
com.example.f08_fragment03;
import
com.example.f08_fragment03.Fragment02.Callback;
import
android.os.Bundle;
import
android.app.Activity;
import
android.app.FragmentManager;
import
android.app.FragmentTransaction;
import
android.view.Menu;
import
android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
public
class
MainActivity
extends
Activity {
private
FragmentManager manager;
// 创建fragment管理
private
FragmentTransaction transaction;
// 创建fragment的事物
private
EditText editText;
private
Button button;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)
this
.findViewById(R.id.editText2);
button = (Button)
this
.findViewById(R.id.button1);
manager = getFragmentManager();
transaction = manager.beginTransaction();
final
Fragment02 fragment02 =
new
Fragment02();
transaction.add(R.id.left, fragment02,
"left"
);
transaction.commit();
// 不管要实现怎样的功能都必须调用以下三个方法
// manager=getFragmentManager();
//transaction=manager.beginTransaction();
// transaction.commit();提交数据
button.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View arg0) {
// TODO Auto-generated method stub
//通过callback机制得到fragment的数据
fragment02.getString(
new
Callback() {
@Override
public
void
getString(String msg) {
// TODO Auto-generated method stub
editText.setText(msg);
}
});
}
});
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return
true
;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
package
com.example.f08_fragment03;
import
android.app.Activity;
import
android.app.Fragment;
import
android.os.Bundle;
import
android.util.Log;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.Button;
import
android.widget.EditText;
public
class
Fragment02
extends
Fragment {
private
Button button;
private
EditText editText;
@Override
public
void
onAttach(Activity activity) {
// TODO Auto-generated method stub
super
.onAttach(activity);
}
@Override
public
void
onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super
.onCreate(savedInstanceState);
}
// 在该方法中更新UI
@Override
public
View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.left,
null
);
button = (Button) view.findViewById(R.id.button1);
editText = (EditText) view.findViewById(R.id.editText1);
button.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View arg0) {
//通过getActivity方法得到activity的控件,以此可以得到数据
EditText eText = (EditText) getActivity().findViewById(
R.id.editText2);
Log.i(
"info"
,
"------->"
+ eText.getText().toString());
editText.setText(eText.getText().toString());
}
});
return
view;
}
@Override
public
void
onStart() {
// TODO Auto-generated method stub
super
.onStart();
}
@Override
public
void
onResume() {
// TODO Auto-generated method stub
super
.onResume();
}
@Override
public
void
onPause() {
// TODO Auto-generated method stub
super
.onPause();
}
//实现数据传递
public
void
getString(Callback callback) {
String msg = editText.getText().toString();
callback.getString(msg);
}
//创建接口
public
interface
Callback {
public
void
getString(String msg);
}
}
|
main的xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<relativelayout tools:context=
".MainActivity"
android:layout_height=
"match_parent"
android:layout_width=
"match_parent"
xmlns:tools=
"http://schemas.android.com/tools"
xmlns:android=
"http://schemas.android.com/apk/res/android"
>
<linearlayout android:id=
"@+id/left"
android:layout_height=
"match_parent"
android:layout_width=
"200dp"
android:orientation=
"vertical"
android:background=
"#cbcbcb"
>
</linearlayout>
<linearlayout android:id=
"@+id/right"
android:layout_height=
"match_parent"
android:layout_width=
"200dp"
android:orientation=
"vertical"
>
</linearlayout>
<edittext android:id=
"@+id/editText2"
android:layout_height=
"wrap_content"
android:layout_width=
"wrap_content"
android:inputtype=
"textPassword"
android:ems=
"10"
android:layout_torightof=
"@+id/left"
android:layout_margintop=
"14dp"
android:layout_alignparenttop=
"true"
>
<requestfocus>
</requestfocus>
<button android:id=
"@+id/button1"
android:layout_height=
"wrap_content"
android:layout_width=
"wrap_content"
android:layout_torightof=
"@+id/left"
android:text=
"得到Fragment的数据"
android:layout_below=
"@+id/editText2"
>
</button></edittext></relativelayout>
|
fragment的xml
1
2
3
4
5
6
7
8
9
10
11
|
<!--?xml version=
"1.0"
encoding=
"utf-8"
?-->
<linearlayout android:layout_height=
"match_parent"
android:layout_width=
"match_parent"
xmlns:android=
"http://schemas.android.com/apk/res/android"
android:orientation=
"vertical"
>
<edittext android:id=
"@+id/editText1"
android:layout_height=
"wrap_content"
android:layout_width=
"match_parent"
android:inputtype=
"text"
android:ems=
"10"
>
<requestfocus>
</requestfocus>
<button android:id=
"@+id/button1"
android:layout_height=
"wrap_content"
android:layout_width=
"wrap_content"
android:text=
"得到Activity的数据"
>
</button></edittext></linearlayout>
|
这样就大功告成了,点击fragment的button就可以在其editText控件显示activity的editText控件的文字,点击activity的button就可以得到fragment的编辑控件的数据!