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:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:gravity = "center"
android:onClick = "showSelectAuthors"
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 = "multipleChoice" />
</ 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package com.example.choicelistviewtest2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class RadioButtonListActivity extends Activity {
private ListView radioButtonList;
private String[] names = new String[] { "芥川龙之介" , "三岛由纪夫" , "川端康成" , "村上春树" ,
"东野圭吾" , "张爱玲" , "金庸" , "钱钟书" , "老舍" , "梁实秋" , "亨利米勒" , "海明威" , "菲兹杰拉德" ,
"凯鲁亚克" , "杰克伦敦" , "小仲马" , "杜拉斯" , "福楼拜" , "雨果" , "巴尔扎克" , "莎士比亚" , "劳伦斯" ,
"毛姆" , "柯南道尔" , "笛福" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioButtonList = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>( this ,
android.R.layout.simple_list_item_multiple_choice, names);
radioButtonList.setAdapter(adapter);
}
public void showSelectAuthors(View v) {
long [] authorsId = radioButtonList.getCheckItemIds();
String name = "" ;
String message;
if (authorsId.length > 0 ) {
// 用户至少选择了一位作家
for ( int i = 0 ; i < authorsId.length; i++) {
name += "," + names[( int ) authorsId[i]];
}
// 将第一个作家前面的“,”去掉
message = name.substring( 1 );
} else {
message = "请至少选择一位作家!" ;
}
Toast.makeText(RadioButtonListActivity. this , message, Toast.LENGTH_LONG)
.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
43
44
45
46
47
48
|
package com.example.choicelistviewtest3;
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) {
//返回每一条Item的Id
return arg0;
}
@Override
public boolean hasStableIds() {
//getCheckedItemIds()方法要求此处返回为真
return true ;
}
@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
|
package com.example.choicelistviewtest3;
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);
}
@Override
public void toggle() {
selectBtn.toggle();
}
} |
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
|
< 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:background = "#fff"
android:orientation = "horizontal" >
< 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 = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignParentRight = "true"
android:layout_centerVertical = "true"
android:layout_gravity = "center_vertical"
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
|
package com.example.choicelistviewtest3;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
public class RadioButtonListActivity extends Activity {
private ListView radioButtonList;
private RadioAdapter adapter;
private String[] authors = new String[] { "芥川龙之介" , "三岛由纪夫" , "川端康成" , "村上春树" ,
"东野圭吾" , "张爱玲" , "金庸" , "钱钟书" , "老舍" , "梁实秋" , "亨利米勒" , "海明威" , "菲兹杰拉德" ,
"凯鲁亚克" , "杰克伦敦" , "小仲马" , "杜拉斯" , "福楼拜" , "雨果" , "巴尔扎克" , "莎士比亚" , "劳伦斯" ,
"毛姆" , "柯南道尔" , "笛福" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_button_list);
radioButtonList = (ListView) findViewById(R.id.list);
adapter = new RadioAdapter( this , authors);
radioButtonList.setAdapter(adapter);
}
public void showSelectAuthors(View v) {
long [] authorsId = radioButtonList.getCheckedItemIds();
String name = "" ;
String message;
if (authorsId.length > 0 ) {
// 用户至少选择了一位作家
for ( int i = 0 ; i < authorsId.length; i++) {
name += "," + authors[( int ) authorsId[i]];
}
// 将第一个作家前面的“,”去掉
message = name.substring( 1 );
} else {
message = "请至少选择一位作家!" ;
}
Toast.makeText(RadioButtonListActivity. this , message, Toast.LENGTH_LONG)
.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
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
|
package com.example.choicelistviewtest2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class RadioButtonListActivity extends Activity {
private ListView radioButtonList;
private String[] names = new String[] { "芥川龙之介" , "三岛由纪夫" , "川端康成" , "村上春树" ,
"东野圭吾" , "张爱玲" , "金庸" , "钱钟书" , "老舍" , "梁实秋" , "亨利米勒" , "海明威" , "菲兹杰拉德" ,
"凯鲁亚克" , "杰克伦敦" , "小仲马" , "杜拉斯" , "福楼拜" , "雨果" , "巴尔扎克" , "莎士比亚" , "劳伦斯" ,
"毛姆" , "柯南道尔" , "笛福" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioButtonList = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>( this ,
android.R.layout.simple_list_item_multiple_choice, names);
radioButtonList.setAdapter(adapter);
}
public void showSelectAuthors(View v) {
// long[] authorsId = radioButtonList.getCheckItemIds();
long [] authorsId = getListSelectededItemIds(radioButtonList);
String name = "" ;
String message;
if (authorsId.length > 0 ) {
// 用户至少选择了一位作家
for ( int i = 0 ; i < authorsId.length; i++) {
name += "," + names[( int ) authorsId[i]];
}
// 将第一个作家前面的“,”去掉
message = name.substring( 1 );
} else {
message = "请至少选择一位作家!" ;
}
Toast.makeText(RadioButtonListActivity. this , message, Toast.LENGTH_LONG)
.show();
}
// 避免使用getCheckItemIds()方法
public long [] getListSelectededItemIds(ListView listView) {
long [] ids = new long [listView.getCount()]; //getCount()即获取到ListView所包含的item总个数
//定义用户选中Item的总个数
int checkedTotal = 0 ;
for ( int i = 0 ; i < listView.getCount(); i++) {
//如果这个Item是被选中的
if (listView.isItemChecked(i)) {
ids[checkedTotal++] = i;
}
}
if (checkedTotal < listView.getCount()) {
//定义选中的Item的ID数组
final long [] selectedIds = new long [checkedTotal];
//数组复制 ids
System.arraycopy(ids, 0 , selectedIds, 0 , checkedTotal);
return selectedIds;
} else {
//用户将所有的Item都选了
return ids;
}
}
} |
更多关于 android_intent 的文章请参考 http://www.itmmd.com/tag/android_intent.html
更多关于 android_Listview的文章请参考 http://www.itmmd.com/tag/android_listview.html
相关推荐
listview多选与单选模式demo 可以进行单选和多选模式切换,一个小demo,请多多指教。
ListView多选操作模式详解CHOICE_MODE_MULTIPLE与CHOICE_MODE_MULTIPLE_MODAL 与该demo配套的文章: http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1105/1906.html
特色填充数据和ListView、GridView用法一样使用Adapter,更新数据直接通过adapter.notifyDataChanged来更新支持点击、单选、多选事件三种模式:FLOW_TAG_CHECKED_NONE、FLOW_TAG_CHECKED_SINGLE、FLOW_TAG_CHECKED_...
Android-ListView该Demo实现了ListView嵌套ListView并还有CheckBox全选反选单选的实类似淘宝购物车功能
此工程包含了listview项中每个按钮的点击的捕获,checkbox的全选、取消多选、反选、删除、编辑、完成等状态;比较类似58同城旧版的操作模式!
5.长按ListView多选删除 6.必要的提醒:例如按两次退出程序 本程序的技术关键点 1.采用了多线程技术以提升程序的性能:主要在MainActivity中的onResume()方法 中进行异步访问数据库来获取列表的信息,这里主要用了...
Android-AbsListView-Drag-And-Drop 具有拖放功能的复杂 ListView 和 GridView。 拖拽项目到下拉菜单视图,回调下拉功能。 滑动选择怎么看? 拖放和MultiChoice(官方和...自定义这是一个自定义的多选模式,你应该
11.12 多选列表对话框 142 11.13 通知:简单、普通、大视图、带进度条、自定义通知 143 12.1 Fragment编写步骤 149 12.2 fragment生命周期演示 150 12.3ScrollView布局文件 156 12.4 Fragment代码 157 12.5 Activity...
Android流式布局,支持点击、单选、多选等,适合用于产品标签等,用法采用Adapter模式,和ListView、GridView用法一样。Android并没有提供流式布局,但是某些场合中,流式布局还是非常适合使用的,比如关键字标签,...
面包车 可能是最强大的RecyclerView框架 ...特色 简洁代码 功能全面 非侵入式 不创建任何文件 刷新不闪屏 数据双向绑定(DataBinding) ...选择模式(多选/单选/全选/取消全选/反选) 拖拽位置 侧滑删除 拖动
FlowTagLayout Android流式布局,支持点击、单选、多选等,适合用于产品标签等,用法采用Adapter模式,和ListView、GridView用法一样!!!
3.消息对话框、确认对话框、列表对话框、单选和多选对话框、自定义对话框 二、对话框的组成(一般有四部分组成) 1.图标 2.标题 3.内容 4.按钮 三、那么如何创建一个对话框呢?(四步组成) 1.第一步创建...