`
wuhua
  • 浏览: 2095050 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Android 设计一个可单选,多选的Demo

阅读更多

Android 提供了相当多的UI,在android.widget 的UI组件库,下面就让我们看看他是ListView的用法吧。

 

下面的Demo 是一个

 

/*
 * Copyright (C) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.example.android.apis.view;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * This example shows how to use choice mode on a list. This list is 
 * in CHOICE_MODE_SINGLE mode, which means the items behave like
 * checkboxes.
 */
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"
    };

 

上面是一个单选的List Demo。

 

对于需要多选的话可以改变这些参数就OK了

 

 

setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, GENRES));

        final ListView listView = getListView();

        listView.setItemsCanFocus(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

很简单吧,Android的开发确实很不错。

 

 

 

2
3
分享到:
评论
3 楼 cnliuyix 2012-04-08  
LZ搞点更有层次的吧,介个一般工程里根本用不到这么简单的。SimpleCursorAdapter加simple_list_item_multichoice再自定义的listitem layout,最后有个listener,这多有意义啊
2 楼 ueseu 2009-09-05  
知道了,CheckedTextView
1 楼 ueseu 2009-09-05  
如果不用android.R.layout.simple_list_item_multiple_choice
怎么实现?

暂时没有android.R.layout.simple_list_item_multiple_choice源码

相关推荐

Global site tag (gtag.js) - Google Analytics