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
|
public
class
MainActivity
extends
Activity
{
CheckedTextView checkedTextView =
null
;
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView1);
final
String[] textViewResource =
new
String[]{
"选项一"
,
"选项二"
,
"选项三"
,
"选项四"
,
"选项五"
};
checkedTextView = (CheckedTextView) findViewById(R.layout.checkedtext);
listView.setAdapter(
new
ArrayAdapter<String>(
this
, R.layout.checkedtext, textViewResource ));
//清单中要配置checkmark语句: android:checkMark="?android:attr/listChoiceIndicatorSingle"
listView.setItemsCanFocus(
false
);
//获得焦点,点击获得事件
//设置单选、多选或者无法选择
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//设置多选
//用setOnItemClickListener监听item点击事件,如果继承listactivity的话,直接重写onItemClick方法即可
listView.setOnItemClickListener(
new
OnItemClickListener()
{
@Override
public
void
onItemClick(AdapterView<?> parent, View view,
int
position,
long
id)
{
Toast.makeText(MainActivity.
this
, textViewResource[position], Toast.LENGTH_SHORT).show();
}
});
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return
true
;
}
}
|
注:
android:checkMark="?android:attr/listChoiceIndicatorSingle"
listView.setItemsCanFocus(
false
);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnItemClickListener()
系统范例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public
class
List10
extends
ListActivity
{
@Override
public
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setListAdapter(
new
ArrayAdapter<String>(
this
,
android.R.layout.simple_list_item_single_choice, GENRES));
final
ListView listView = getListView();
listView.setItemsCanFocus(
false
);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
private
static
final
String[] GENRES =
new
String[]
{
"Action"
,
"Adventure"
,
"Animation"
,
"Children"
,
"Comedy"
,
"Documentary"
,
"Drama"
,
"Foreign"
,
"History"
,
"Independent"
,
"Romance"
,
"Sci-Fi"
,
"Television"
,
"Thriller"
};
}
|
本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1202966,如需转载请自行联系原作者