`
xiaoming123123
  • 浏览: 100342 次
  • 性别: Icon_minigender_1
  • 来自: 南昌
社区版块
存档分类
最新评论

listview实现单选功能

阅读更多
package com.xiaoming;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MainActivity extends Activity {

	private static final String TAG = "WZP13_RadioGroupActivity";
	private ListView listView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		listView = (ListView) this.findViewById(R.id.list);
		MyAdapter adapter = new MyAdapter();
		listView.setAdapter(adapter);
	}

	private class MyAdapter extends BaseAdapter {
		private String[] s = new String[] { "小天AAA", "小天BBB", "小天CCC", "小天DDD"};
		private int temp = -1;
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return s.length;
		}
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			convertView = MainActivity.this.getLayoutInflater()
					.inflate(R.layout.item, null);
			Button button = (Button) convertView.findViewById(R.id.button);
			button.setText(s[position]);
			RadioButton radioButton = (RadioButton) convertView
					.findViewById(R.id.radioButton);
			radioButton.setId(position);
			radioButton
					.setOnCheckedChangeListener(new OnCheckedChangeListener() {
						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							if (isChecked) {
								if (temp != -1) {
									RadioButton tempButton = (RadioButton) MainActivity.this
											.findViewById(temp);
									if (tempButton != null) {
										tempButton.setChecked(false);
									}
								}
								temp = buttonView.getId();
								Log.i(TAG, "you are women- -   " + isChecked
										+ "   " + temp);
							}
						}
					});
			if (temp == position) {
				radioButton.setChecked(true);
			}
			return convertView;
		}
	}
}


布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试" >
    </Button>

    <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>


适配器不在acitivyt里面,则把activity传到适配器里面去
分享到:
评论

相关推荐

    gridview和listview实现单选功能

    自定义gridview和listview实现单选功能实现,通过adapter来控制。

    android listView 单选 功能

    在许多场景下,我们需要为ListView实现单选功能,即用户点击某一条目后,该条目会被选中并显示选中状态,比如一个对勾图标。本文将详细探讨如何在Android中实现ListView的单选功能。 一、ListView的基础 ListView是...

    ListView实现单选

    本篇文章将详细讲解如何在Android中通过ListView实现单选功能。 首先,我们需要一个包含单选按钮的自定义布局。在XML布局文件中,可以创建一个`CheckedTextView`或者`RadioButton`作为列表项的视图。例如: ```xml...

    android 自定义ListView实现单选

    本篇文章将详细探讨如何通过自定义ListView来实现单选功能,旨在帮助开发者解决这一问题。 一、自定义ListView的必要性 系统自带的ListView默认并未提供单选模式,而是多选或全选。当用户需要在列表中选择一个项目...

    ListView实现单选多选

    本示例主要讲解如何在ListView中实现单选和多选功能。这在很多应用场景中非常常见,例如在设置菜单、联系人选择等场景。 首先,我们需要理解ListView的基本工作原理。ListView通过Adapter将数据与视图进行绑定,...

    ListView控件使用CheckBoxes属性实现单选功能

    本篇文章将详细介绍如何利用ListView控件的CheckBoxes属性来实现单选功能。 首先,我们需要创建一个WinForm应用程序,并在窗体上添加一个ListView控件。在设计界面中,可以通过属性窗口设置ListView的CheckBoxes...

    delphi Listview复选框checkbox实现单选的功能

    本示例着重讲解如何在ListView中实现复选框的单选功能。 在Delphi中,ListView的复选框功能可以通过设置`ListView.Style`属性为`vsReport`来开启。默认情况下,ListView并不支持复选框的单选模式,我们需要自定义...

    listview单选实现

    ListView是Android平台中一个常用的控件,用于展示大量的数据列表。...通过这个例子,你可以学习到ListView与Adapter的配合,以及如何利用RadioButton实现单选功能,这些都是Android开发中常用的技术。

    delphi中listview启用checkbox实现单选

    本文将深入探讨如何在Delphi中启用ListView的复选框并实现单选功能,特别针对Delphi7版本。 首先,我们需要了解ListView的基本用法。在Delphi的工具箱中找到ListView控件,将其拖放到表单上。在属性面板中,可以...

    ListView实现单选多选.zip

    本压缩包文件"ListView实现单选多选.zip"显然关注的是如何在ListView中实现单选和多选功能,这对于创建具有交互性的应用是非常重要的。下面将详细讲解实现这一功能所需的知识点。 1. **ListView的基本概念**: - ...

    android ListView中RadioinButton实现单选

    这是我平时用的多的一个小项目,刚开始的时候,老是实现不了在listView中RadionButton的单选功能,这是后来经过多次使用后。终于找到的解决listVIew中单选问题的解决办法,弄出来和大家一起学习

    android ListView+CheckBox实现单选

    为了实现单选功能,我们需要一个机制来跟踪当前被选中的项,并确保每次只能有一个选项处于选中状态。 1. 创建数据模型: 首先,创建一个数据模型类,它包含两个字段:一个是存储数据的字段,另一个是表示选择状态...

    ListView+iCheckBox实现单选

    在本主题“ListView+iCheckBox实现单选”中,我们将探讨如何将这两个组件结合,以实现用户在ListView中只能选择一个条目的功能。 首先,我们需要创建一个自定义的ListView适配器,这个适配器应该包含iCheckBox,...

    Android ListView实现单选及多选等功能示例

    "Android ListView实现单选及多选等功能示例" Android ListView是Android系统中最常用的控件之一,用于显示大量数据列表。然而,在实际开发中,我们经常需要实现ListView的单选、多选、长按多选等功能。本文将详细...

    listview_单选多选

    在这个"listview_单选多选"的主题中,我们将深入探讨ListView如何实现单选、多选功能,以及相关的编程实践。 首先,ListView的选择器通常是一个颜色、图像或者形状的组合,通过`android:background`属性设置在...

    ListView的单选和多选

    实现单选功能 #### 2.1 单选模式配置 在ListView中实现单选,通常采用CheckedTextView或者在每个列表项中添加一个CheckBox。通过设置单选模式,当用户点击某一项时,其他项自动取消选中。 ```java listView....

    (Android)五分钟让你轻松学会ListView单选和多选功能

    本教程将深入讲解如何在ListView中实现单选和多选功能,让你在短短的五分钟内掌握这一关键技能。 首先,我们要理解ListView的基本结构。ListView是由多个View(称为Item视图)组成的,每个Item视图通常对应数据集中...

    Android ListView单选

    在某些应用场景中,我们可能需要实现ListView的单选功能,即用户只能选择一个条目,每次点击其他条目时,之前的选择会被取消。本篇将详细介绍如何在Android中实现ListView的单选模式。 一、ListView的基本使用 首先...

    ListView中使用单选和多选效果

    本篇文章将详细讲解如何在ListView中实现单选和多选效果。 首先,我们需要了解ListView的基本结构。ListView通常与Adapter结合使用,Adapter负责将数据转化为ListView可显示的View。在单选和多选场景下,我们可能...

Global site tag (gtag.js) - Google Analytics