Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c27cb9b

Browse files
committed
添加了一个LoginFragment来测试fragment之间的焦点移动问题
1. 如果是只有一个fragment,focus是正常的,activity会收到fragment中不处理的按键,比如back.处理了按键不会收到 2. 如果有两个fragments,focus会受系统的控制,在上下两个中进行切换 3. 如果要拦截系统focus切换,可以在onKeyDown中来进行判断,如果focus在button A上,就不再让系统切换focus,这里是这样来处理的,根据fragment的onKeyDown的返回值来做的: * 0 : 表示这个消息我不管,返给activity就好了 * 1 : 表示这个消息我处理了,不让activity再处理 * 2 : 表示这个消息我不处理,也不让activity来处理,给系统来处理
1 parent b5ea606 commit c27cb9b

File tree

5 files changed

+400
-72
lines changed

5 files changed

+400
-72
lines changed

mycode/projects/FragmentTest/res/layout/activity_main.xml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,26 @@
22
android:layout_width="match_parent"
33
android:layout_height="match_parent"
44
android:background="#000000"
5-
android:gravity="center_horizontal"
5+
android:gravity="center_horizontal|center_vertical"
66
android:orientation="vertical" >
7+
<LinearLayout
8+
android:id="@+id/mylogin_container"
9+
android:layout_width="400dp"
10+
android:layout_height="200dp"
11+
android:background="#ff555555"
12+
android:gravity="center_horizontal"
13+
android:orientation="vertical" >
14+
15+
<fragment
16+
android:id="@+id/login_fragment"
17+
android:name="com.example.fragmenttest.LoginFragment"
18+
android:layout_width="wrap_content"
19+
android:layout_height="wrap_content" />
20+
</LinearLayout>
721

822
<LinearLayout
923
android:layout_width="match_parent"
10-
android:layout_height="match_parent"
24+
android:layout_height="0dp"
1125
android:layout_weight="1"
1226
android:gravity="center_horizontal"
1327
android:orientation="vertical" >
@@ -18,7 +32,7 @@
1832
android:layout_width="wrap_content"
1933
android:layout_height="wrap_content" />
2034
</LinearLayout>
21-
35+
<!--
2236
<LinearLayout
2337
android:layout_width="match_parent"
2438
android:layout_height="match_parent"
@@ -55,5 +69,5 @@
5569
android:gravity="center_horizontal|center_vertical" >
5670
</FrameLayout>
5771
</LinearLayout>
58-
72+
-->
5973
</LinearLayout>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:layout_width="match_parent"
3+
android:layout_height="match_parent"
4+
android:background="#000000"
5+
android:gravity="center_horizontal"
6+
android:orientation="vertical" >
7+
8+
<TableLayout
9+
android:layout_width="match_parent"
10+
android:layout_height="wrap_content" >
11+
12+
<TableRow
13+
android:id="@+id/tableRow1"
14+
android:layout_width="wrap_content"
15+
android:layout_height="wrap_content" >
16+
17+
<TextView
18+
android:layout_width="120dp"
19+
android:layout_height="40dp"
20+
android:text="Name"
21+
android:textAlignment="textEnd"
22+
android:textSize="26dp" />
23+
24+
<EditText
25+
android:id="@+id/nameEdit"
26+
android:layout_width="200dp"
27+
android:layout_height="40dp" />
28+
</TableRow>
29+
30+
<TableRow
31+
android:id="@+id/tableRow2"
32+
android:layout_width="wrap_content"
33+
android:layout_height="wrap_content" >
34+
35+
<TextView
36+
android:layout_width="120dp"
37+
android:layout_height="40dp"
38+
android:text="Age"
39+
android:textAlignment="textEnd"
40+
android:textSize="26dp" />
41+
42+
<EditText
43+
android:id="@+id/ageEdit"
44+
android:layout_width="200dp"
45+
android:layout_height="40dp"
46+
android:nextFocusDown="@+id/btnCommit" />
47+
</TableRow>
48+
</TableLayout>
49+
50+
<LinearLayout
51+
android:layout_width="400dp"
52+
android:layout_height="wrap_content"
53+
android:orientation="horizontal"
54+
android:padding="10dp" >
55+
56+
<Button
57+
android:id="@+id/btnCommit"
58+
android:layout_width="100dp"
59+
android:layout_height="wrap_content"
60+
android:layout_marginLeft="10dp"
61+
android:text="Commit" />
62+
63+
<Button
64+
android:id="@+id/btnCancel"
65+
android:layout_width="100dp"
66+
android:layout_height="wrap_content"
67+
android:layout_marginRight="10dp"
68+
android:text="Cancel" />
69+
</LinearLayout>
70+
71+
</LinearLayout>
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="fill_parent"
4+
android:layout_height="fill_parent"
5+
android:orientation="vertical"
6+
android:padding="3dip" >
7+
8+
<!-- 第1个TableLayout,用于描述表中的列属性。第0列可伸展,第1列可收缩,第2列被隐藏 -->
9+
10+
<TextView
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"
13+
android:background="#7f00ffff"
14+
android:text="表1:全局设置:列属性设置"
15+
android:textSize="15sp" />
16+
17+
<TableLayout
18+
android:id="@+id/table1"
19+
android:layout_width="fill_parent"
20+
android:layout_height="wrap_content"
21+
android:collapseColumns="2"
22+
android:padding="3dip"
23+
android:shrinkColumns="1"
24+
android:stretchColumns="0" >
25+
26+
<TableRow>
27+
28+
<Button android:text="该列可伸展" />
29+
30+
<Button android:text="该列可收缩" />
31+
32+
<Button android:text="我被隐藏了" />
33+
</TableRow>
34+
35+
<TableRow>
36+
37+
<TextView android:text="我向行方向伸展,我可以很长 " />
38+
39+
<TextView android:text="我向列方向收缩,我可以很深" />
40+
</TableRow>
41+
</TableLayout>
42+
43+
<!-- 第2个TableLayout,用于描述表中单元格的属性,包括:android:layout_column 及android:layout_span -->
44+
45+
<TextView
46+
android:layout_width="wrap_content"
47+
android:layout_height="wrap_content"
48+
android:background="#7f00ffff"
49+
android:text="表2:单元格设置:指定单元格属性设置"
50+
android:textSize="15sp" />
51+
52+
<TableLayout
53+
android:id="@+id/table2"
54+
android:layout_width="fill_parent"
55+
android:layout_height="wrap_content"
56+
android:padding="3dip" >
57+
58+
<TableRow>
59+
60+
<Button android:text="第0列" />
61+
62+
<Button android:text="第1列" />
63+
64+
<Button android:text="第2列" />
65+
</TableRow>
66+
67+
<TableRow>
68+
69+
<TextView
70+
android:layout_column="1"
71+
android:text="我被指定在第1列" />
72+
</TableRow>
73+
74+
<TableRow>
75+
76+
<TextView
77+
android:layout_column="1"
78+
android:layout_span="2"
79+
android:text="我跨1到2列,不信你看!" />
80+
</TableRow>
81+
</TableLayout>
82+
83+
<!-- 第3个TableLayout,使用可伸展特性布局 -->
84+
85+
<TextView
86+
android:layout_width="wrap_content"
87+
android:layout_height="wrap_content"
88+
android:background="#7f00ffff"
89+
android:text="表3:应用一,非均匀布局"
90+
android:textSize="15sp" />
91+
92+
<TableLayout
93+
android:id="@+id/table3"
94+
android:layout_width="fill_parent"
95+
android:layout_height="wrap_content"
96+
android:padding="3dip"
97+
android:stretchColumns="*" >
98+
99+
<TableRow>
100+
101+
<Button android:text="" >
102+
</Button>
103+
104+
<Button android:text="两字" >
105+
</Button>
106+
107+
<Button android:text="三个字" >
108+
</Button>
109+
</TableRow>
110+
</TableLayout>
111+
112+
<!-- 第4个TableLayout,使用可伸展特性,并指定每个控件宽度一致,如1dip -->
113+
114+
<TextView
115+
android:layout_width="wrap_content"
116+
android:layout_height="wrap_content"
117+
android:background="#7f00ffff"
118+
android:text="表4:应用二,均匀布局"
119+
android:textSize="15sp" />
120+
121+
<TableLayout
122+
android:id="@+id/table4"
123+
android:layout_width="fill_parent"
124+
android:layout_height="wrap_content"
125+
android:padding="3dip"
126+
android:stretchColumns="*" >
127+
128+
<TableRow>
129+
130+
<Button
131+
android:layout_width="1dip"
132+
android:text="" >
133+
</Button>
134+
135+
<Button
136+
android:layout_width="1dip"
137+
android:text="两字" >
138+
</Button>
139+
140+
<Button
141+
android:layout_width="1dip"
142+
android:text="三个字" >
143+
</Button>
144+
</TableRow>
145+
</TableLayout>
146+
147+
</LinearLayout>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.example.fragmenttest;
2+
3+
import android.app.Fragment;
4+
import android.os.Bundle;
5+
import android.view.KeyEvent;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.View.OnClickListener;
9+
import android.view.ViewGroup;
10+
import android.widget.Button;
11+
import android.widget.Toast;
12+
13+
public class LoginFragment extends Fragment {
14+
@Override
15+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
16+
Bundle savedInstanceState) {
17+
View view = inflater.inflate(R.layout.fragment_login,container,false);
18+
Button btnCommit = (Button) view.findViewById(R.id.btnCommit);
19+
Button btnCancel = (Button) view.findViewById(R.id.btnCancel);
20+
btnCommit.setOnClickListener(new OnClickListener() {
21+
@Override
22+
public void onClick(View v) {
23+
// TODO Auto-generated method stub
24+
Toast.makeText(getActivity(), "Commit Clicked!", Toast.LENGTH_SHORT).show();
25+
}
26+
});
27+
btnCancel.setOnClickListener(new OnClickListener() {
28+
@Override
29+
public void onClick(View v) {
30+
// TODO Auto-generated method stub
31+
Toast.makeText(getActivity(), "Cancel Clicked!", Toast.LENGTH_SHORT).show();
32+
}
33+
});
34+
/*
35+
view.setOnKeyListener(new OnKeyListener() {
36+
37+
@Override
38+
public boolean onKey(View v, int keyCode, KeyEvent event) {
39+
// TODO Auto-generated method stub
40+
return false;
41+
}
42+
});*/
43+
return view;
44+
}
45+
/*
46+
* fragment 没有自己的onKeyDown函数,这个函数我们会在MainActivity的onKeyDown
47+
* 中进行调用,返回值
48+
* 0 : 表示这个消息我不管,返给activity就好了
49+
* 1 : 表示这个消息我处理了,不让activity再处理
50+
* 2 : 表示这个消息我不处理,也不让activity来处理,给系统来处理
51+
*/
52+
int onKeyDown(int keyCode, KeyEvent event) {
53+
54+
/*
55+
* 这里是把返回键直接给activity来处理,如果activity的没有处理
56+
* 而直接给系统,则整个app 会退出,一般会做处理,必然隐藏这个fragment
57+
* 其实如果fragment是动态加载的话,在这里可以自动隐藏
58+
*/
59+
//if(keyCode == KeyEvent.KEYCODE_BACK)
60+
// return 0;
61+
62+
/*
63+
* 添加下面的处理是在commit cancel按钮不再往下移动focus,测试OK
64+
*/
65+
if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN){
66+
View view = getView();
67+
Button btnCommit = (Button) view.findViewById(R.id.btnCommit);
68+
Button btnCancel = (Button) view.findViewById(R.id.btnCancel);
69+
if(btnCancel.isFocused() || btnCommit.isFocused()){
70+
return 1;
71+
}
72+
return 2;
73+
}
74+
return 2;
75+
}
76+
}

0 commit comments

Comments
 (0)