1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
xmlns:tools
=
"http://schemas.android.com/tools"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:orientation
=
"vertical"
>
<
Button
android:id
=
"@+id/select"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:gravity
=
"center"
android:text
=
"@string/select_authors"
android:textSize
=
"25sp"
/>
<
ListView
android:id
=
"@+id/list"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:choiceMode
=
"singleChoice"
/>
</
LinearLayout
>
|
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
|
<
RelativeLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
xmlns:tools
=
"http://schemas.android.com/tools"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:orientation
=
"horizontal"
android:background
=
"#fff"
>
<
TextView
android:id
=
"@+id/author"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignParentLeft
=
"true"
android:layout_centerVertical
=
"true"
android:padding
=
"10dp"
android:textSize
=
"20sp"
/>
<
CheckBox
android:id
=
"@+id/radio"
android:layout_width
=
"20dp"
android:layout_height
=
"20dp"
android:layout_alignParentRight
=
"true"
android:layout_centerVertical
=
"true"
android:layout_gravity
=
"center_vertical"
android:layout_marginRight
=
"10dp"
android:background
=
"@drawable/radio_button_normal"
android:button
=
"@null"
android:clickable
=
"false"
android:focusable
=
"false"
android:focusableInTouchMode
=
"false"
android:padding
=
"10dp"
/>
</
RelativeLayout
>
|
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
|
package
com.example.choicelistviewtest;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.AdapterView;
import
android.widget.Toast;
import
android.widget.AdapterView.OnItemClickListener;
import
android.widget.ListView;
public
class
RadioButtonListActivity
extends
Activity {
private
ListView radioButtonList;
private
RadioAdapter adapter;
// 模拟几个数据,作为List的条目
private
String[] authors = {
"芥川龙之介"
,
"三岛由纪夫"
,
"川端康成"
,
"村上春树"
,
"东野圭吾"
,
"张爱玲"
,
"金庸"
,
"钱钟书"
,
"老舍"
,
"梁实秋"
,
"亨利米勒"
,
"海明威"
,
"菲兹杰拉德"
,
"凯鲁亚克"
,
"杰克伦敦"
,
"小仲马"
,
"杜拉斯"
,
"福楼拜"
,
"雨果"
,
"巴尔扎克"
,
"莎士比亚"
,
"劳伦斯"
,
"毛姆"
,
"柯南道尔"
,
"笛福"
};
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_choice_list_view_test);
radioButtonList = (ListView) findViewById(R.id.list);
adapter =
new
RadioAdapter(
this
, authors);
radioButtonList.setAdapter(adapter);
radioButtonList.setOnItemClickListener(
new
OnItemClickListener() {
@Override
public
void
onItemClick(AdapterView<?> arg0, View arg1,
int
arg2,
long
arg3) {
Toast.makeText(RadioButtonListActivity.
this
,
"您选择的作家是:"
+ authors[arg2], Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.select).setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
int
select = radioButtonList.getCheckedItemPosition();
// INVALID_POSITION 代表无效的位置。有效值的范围是 0 到当前适配器项目数减 1 。
if
(ListView.INVALID_POSITION != select) {
Toast.makeText(RadioButtonListActivity.
this
,
"您选择的作家是:"
+ authors[select], Toast.LENGTH_SHORT)
.show();
}
else
{
// 如果用户开始没有选择
Toast.makeText(RadioButtonListActivity.
this
,
"请选择一位作家!"
,
Toast.LENGTH_SHORT).show();
}
}
});
}
}
|
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
|
package
com.example.choicelistviewtest;
import
android.content.Context;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.BaseAdapter;
public
class
RadioAdapter
extends
BaseAdapter {
private
String[] authors;
private
Context c;
public
RadioAdapter(Context c, String[] authors) {
super
();
this
.c = c;
this
.authors = authors;
}
@Override
public
int
getCount() {
return
authors.length;
}
@Override
public
Object getItem(
int
arg0) {
return
null
;
}
@Override
public
long
getItemId(
int
arg0) {
return
0
;
}
@Override
public
View getView(
int
arg0, View arg1, ViewGroup arg2) {
ChoiceListItemView choiceListItemView =
new
ChoiceListItemView(c,
null
);
choiceListItemView.setName(authors[arg0]);
return
choiceListItemView;
}
}
|
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
|
package
com.example.choicelistviewtest;
import
android.content.Context;
import
android.util.AttributeSet;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.widget.CheckBox;
import
android.widget.Checkable;
import
android.widget.LinearLayout;
import
android.widget.TextView;
public
class
ChoiceListItemView
extends
LinearLayout
implements
Checkable {
private
TextView nameTxt;
private
CheckBox selectBtn;
public
ChoiceListItemView(Context context, AttributeSet attrs) {
super
(context, attrs);
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.item_list,
this
,
true
);
nameTxt = (TextView) v.findViewById(R.id.author);
selectBtn = (CheckBox) v.findViewById(R.id.radio);
}
public
void
setName(String text) {
nameTxt.setText(text);
}
@Override
public
boolean
isChecked() {
return
selectBtn.isChecked();
}
@Override
public
void
setChecked(
boolean
checked) {
selectBtn.setChecked(checked);
//根据是否选中来选择不同的背景图片
if
(checked) {
selectBtn.setBackgroundResource(R.drawable.radio_button_checked);
}
else
{
selectBtn.setBackgroundResource(R.drawable.radio_button_normal);
}
}
@Override
public
void
toggle() {
selectBtn.toggle();
}
}
|