Android:实现类似RadioButton自动换行及单选效果

最近做一个项目,有很多项需要单选,但android上RadioGroup不能实现RadioButton自动换行的效果,于是通过另一种方式来实现类似效果.

package com.dq.slidemenu;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class SearchMovieActivity extends Activity {
	private GridView gridViewType;
	private GridView gridViewArea;
	private GridView gridViewTime;
	private TextView title;
	private Button back;
	private Button rightBT;
	private OnClickListener MyOnClickListener;
	private SimpleAdapter adater1;
	private SimpleAdapter adater2;
	private SimpleAdapter adater3;
	private ArrayList<HashMap<String, Object>> mList1;
	private ArrayList<HashMap<String, Object>> mList2;
	private ArrayList<HashMap<String, Object>> mList3;
	private static final int listContent1[] = {R.string.search_type_all,R.string.search_type_love,
		R.string.search_type_biography,R.string.search_type_cartoon,R.string.search_type_action,
		R.string.search_type_ancient,R.string.search_type_child,R.string.search_type_dance,
		R.string.search_type_family,R.string.search_type_frightened,R.string.search_type_terror,
		R.string.search_type_fiction,R.string.search_other};
	private static final int listContent2[] = {R.string.search_type_all,R.string.search_area_china,
		R.string.search_area_china_hk,R.string.search_area_usa,R.string.search_area_europe,
		R.string.search_area_kj,R.string.search_area_tailand,R.string.search_other};
	private static final int listContent3[] = {R.string.search_time_all,R.string.search_time_2013,
		R.string.search_time_2012,R.string.search_time_2011,R.string.search_time_2010,
		R.string.search_time_2000,R.string.search_time_90,R.string.search_time_other};
	private static final int TYPE_NUM = 13;
	private static final int AREA_NUM = 8;
	private static final int TIME_NUM = 8;
	private int typeLastItem = 0;
	private int areaLastItem = 0;
	private int timeLastItem = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initMainUI();
		initMainUIListener();
	}

	private void initMainUI() {
		setContentView(R.layout.activity_search2);
		title = (TextView)findViewById(R.id.title_activity);
		if (title != null) {
			title.setText(R.string.search_movie);
		}
		back = (Button)findViewById(R.id.back);
		if (back != null) {
			back.setVisibility(View.VISIBLE);
		}
		rightBT = (Button)findViewById(R.id.rightBT);
		if (rightBT != null) {
			rightBT.setVisibility(View.VISIBLE);
			rightBT.setText(R.string.search);
		}
		initGridViewOne();
		initGridViewTwo();
		initGridViewThree();
	}

	private void initGridViewOne() {
		gridViewType = (GridView)findViewById(R.id.search_type);
		mList1 = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < TYPE_NUM; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("radioIcon", R.drawable.radiobutton_off);
			map.put("radioText", getString(listContent1[i]));
			mList1.add(map);
		}
		adater1 = new SimpleAdapter(getApplicationContext(), mList1, R.layout.search_grid_item,
				new String[]{"radioIcon","radioText"}, new int[]{R.id.item_RadioImg,R.id.item_RadioText});
		if (gridViewType != null) {
			gridViewType.setAdapter(adater1);
			gridViewType.requestFocus();
			gridViewType.setOnItemClickListener(new MyTypeOnItemClick());
		}
	}

	private void initGridViewTwo() {
		gridViewArea = (GridView)findViewById(R.id.search_area);
		mList2 = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < AREA_NUM; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("radioIcon", R.drawable.radiobutton_off);
			map.put("radioText", getString(listContent2[i]));
			mList2.add(map);
		}
		adater2 = new SimpleAdapter(getApplicationContext(), mList2, R.layout.search_grid_item,
				new String[]{"radioIcon","radioText"}, new int[]{R.id.item_RadioImg,R.id.item_RadioText});
		if (gridViewArea != null) {
			gridViewArea.setAdapter(adater2);
			gridViewArea.requestFocus();
			gridViewArea.setOnItemClickListener(new MyAreaOnItemClick());
		}
	}

	private void initGridViewThree() {
		gridViewTime = (GridView)findViewById(R.id.search_time);
		mList3 = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < TIME_NUM; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("radioIcon", R.drawable.radiobutton_off);
			map.put("radioText", getString(listContent3[i]));
			mList3.add(map);
		}
		adater3 = new SimpleAdapter(getApplicationContext(), mList3, R.layout.search_grid_item,
				new String[]{"radioIcon","radioText"}, new int[]{R.id.item_RadioImg,R.id.item_RadioText});
		if (gridViewTime != null) {
			gridViewTime.setAdapter(adater3);
			gridViewTime.requestFocus();
			gridViewTime.setOnItemClickListener(new MyTimeOnItemClick());
		}
	}

	private void goToSearch() {
		LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.search_result, null);
		setContentView(layout);
		title = (TextView)layout.findViewById(R.id.title_activity);
		if (title != null) {
			title.setText(R.string.search_result);
		}
		back = (Button)layout.findViewById(R.id.back);
		if (back != null) {
			back.setVisibility(View.VISIBLE);
			back.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					finish();
				}

			});
		}
	}

	private void initMainUIListener() {
		MyOnClickListener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switch(v.getId()) {
				case R.id.back:
					finish();
					break;
				case R.id.rightBT:
					goToSearch();
					break;
				default:
					break;
				}
			}
		};
		if (back != null) {
			back.setOnClickListener(MyOnClickListener);
		}
		if (rightBT != null) {
			rightBT.setOnClickListener(MyOnClickListener);
		}
	}

	private void changeItemImg(SimpleAdapter sa, int selectedItem, boolean isOn) {
		HashMap<String, Object> map = (HashMap<String, Object>)sa.getItem(selectedItem);
		if (isOn) {
			map.put("radioIcon", R.drawable.radiobutton_on);
		} else {
			map.put("radioIcon", R.drawable.radiobutton_off);
		}
		sa.notifyDataSetChanged();
	}

	private class MyTypeOnItemClick implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			System.out.println("search_type");
			if (typeLastItem != position) {
				if (typeLastItem >= 0) {
					changeItemImg(adater1, typeLastItem, false);
				}
			}
			typeLastItem = position;
			changeItemImg(adater1, position, true);
			HashMap<String, Object> map = (HashMap<String, Object>)adater1.getItem(position);
			String typeStr = (String)map.get("radioText");
			System.out.println("Type string:"+typeStr);
		}
	}

	private class MyAreaOnItemClick implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			System.out.println("search_area");
			if (areaLastItem != position) {
				if (areaLastItem >= 0) {
					changeItemImg(adater2, areaLastItem, false);
				}
			}
			areaLastItem = position;
			changeItemImg(adater2, position, true);
			HashMap<String, Object> map = (HashMap<String, Object>)adater2.getItem(position);
			String areaStr = (String)map.get("radioText");
			System.out.println("Area string:"+areaStr);
		}
	}

	private class MyTimeOnItemClick implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			System.out.println("search_time");
			if (timeLastItem != position) {
				if (areaLastItem >= 0) {
					changeItemImg(adater3, timeLastItem, false);
				}
			}
			timeLastItem = position;
			changeItemImg(adater3, position, true);
			HashMap<String, Object> map = (HashMap<String, Object>)adater3.getItem(position);
			String timeStr = (String)map.get("radioText");
			System.out.println("Time string:"+timeStr);
		}
	}

}

2. [文件] activity_search2.xml ~ 5KB     下载(31)     

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/main_bg">
    <!-- titlebar -->
    <include
        android:id="@+id/head"
       	layout="@layout/head"
        />
		<RelativeLayout
		    android:layout_width="fill_parent"
		    android:layout_height="fill_parent"
		    android:layout_below="@+id/head">
		<!-- 类型 -->
			<LinearLayout
				    android:id="@+id/linearOne"
			        android:layout_width="fill_parent"
			        android:layout_height="wrap_content"
			        android:orientation="vertical"
			        >
		        <TextView
		            android:id="@+id/leixing"
		            android:layout_width="wrap_content"
		            android:layout_height="wrap_content"
		            android:text="@string/search_type_one"
		            android:textSize="18.0sp"
					android:textColor="#FF1EA5E4"
		            />
		        <View
		            android:id="@+id/line1"
		            android:layout_width="fill_parent"
		            android:layout_height="1px"
		            android:background="@drawable/p6"
		            />
		        <GridView
		            android:id="@+id/search_type"
		            android:layout_width="fill_parent"
		            android:layout_height="wrap_content"
		            android:numColumns="auto_fit"
			        android:verticalSpacing="10dp"
			        android:horizontalSpacing="1dp"
			        android:columnWidth="60dp"
			        android:stretchMode="columnWidth"
			        android:gravity="center"
			        android:layout_marginTop="5dip"
		            >
		        </GridView>
		    </LinearLayout>
		<!-- 地区 -->
			<LinearLayout
			    android:id="@+id/linearTwo"
		        android:layout_width="fill_parent"
		        android:layout_height="wrap_content"
		        android:layout_below="@+id/linearOne"
		        android:orientation="vertical"
		        >
		        <View
		            android:layout_width="fill_parent"
		            android:layout_height="1px"
		            android:background="@drawable/p6"
		            />
		        <TextView
		            android:id="@+id/area"
		            android:layout_width="wrap_content"
		            android:layout_height="wrap_content"
		            android:text="@string/search_type_two"
		            android:textSize="18.0sp"
					android:textColor="#FF1EA5E4"
		            />
		        <View
		            android:layout_width="fill_parent"
		            android:layout_height="1px"
		            android:background="@drawable/p6"
		            />
		        <GridView
		            android:id="@+id/search_area"
		            android:layout_width="fill_parent"
		            android:layout_height="wrap_content"
		            android:numColumns="auto_fit"
			        android:verticalSpacing="10dp"
			        android:horizontalSpacing="1dp"
			        android:columnWidth="60dp"
			        android:stretchMode="columnWidth"
			        android:gravity="center"
			        android:layout_marginTop="5dip"
		            >
		        </GridView>
		    </LinearLayout>
		<!-- 年份 -->
			<LinearLayout
			    android:id="@+id/linearThree"
		        android:layout_width="fill_parent"
		        android:layout_height="wrap_content"
		        android:layout_below="@+id/linearTwo"
		        android:orientation="vertical"
		        >
		        <View
		            android:layout_width="fill_parent"
		            android:layout_height="1px"
		            android:background="@drawable/p6"
		            />
		        <TextView
		            android:id="@+id/time"
		            android:layout_width="wrap_content"
		            android:layout_height="wrap_content"
		            android:text="@string/search_type_three"
		            android:textSize="18.0sp"
					android:textColor="#FF1EA5E4"
		            />
		        <View
		            android:layout_width="fill_parent"
		            android:layout_height="1px"
		            android:background="@drawable/p6"
		            />
		        <GridView
		            android:id="@+id/search_time"
		            android:layout_width="fill_parent"
		            android:layout_height="wrap_content"
		            android:numColumns="auto_fit"
			        android:verticalSpacing="10dp"
			        android:horizontalSpacing="1dp"
			        android:columnWidth="85dp"
			        android:stretchMode="columnWidth"
			        android:gravity="center"
			        android:layout_marginTop="5dip"
		            >
		        </GridView>
		        <View
		            android:layout_width="fill_parent"
		            android:layout_height="1px"
		            android:background="@drawable/p6"
		            />
		    </LinearLayout>
		</RelativeLayout>
</RelativeLayout>

4. [图片] 1.png    

时间: 2024-11-03 22:14:04

Android:实现类似RadioButton自动换行及单选效果的相关文章

Android实现类似微信的文本输入框 效果

本文内容比较简单,给大家介绍一下微信的文本输入框是如何实现的,其实那只是个普通的文本框设了一个特殊的背景而已.具体微信怎么实现的,大家可以反编译下,这里介绍下如何实现这个背景. 可以先看下文章末尾的效果图,里面的文本框是不是和微信的比较像啊,下面说下实现思想: 首先,这种效果用.9图我不知道是否可以做出来.如果不用.9图的话,那就只能用drawable来写,这种drawable有点复杂,可以采用LayerList来实现,我的思想如下: 分三层实现,这里假设activity的背景是白色,第一层也(

android中怎么在gridview中实现像radiogroup那样的单选效果呢?

问题描述 android中怎么在gridview中实现像radiogroup那样的单选效果呢? android中怎么在gridview中实现像radiogroup那样的单选效果呢?我的Item布局里只是一个简单的Button,就想实现像radioButton那样的单选效果,但选中某个Item时,那个Button会变成红色. 解决方案 你用一个变量记录点击的item,然后,再次点击的时候做判断就行了. 解决方案二: 保存每个item的状态,或者在你gridview对应的实体类中增加一个字段来标志选

button 单选-android ListView中的Item有两行Button,要求实现单选效果,应该怎么做呢?

问题描述 android ListView中的Item有两行Button,要求实现单选效果,应该怎么做呢? 就是向这样的Button,在ListView的Item中,如果有两行,要能实现单选效果,应该怎么做呢?麻烦各位大神们说详细点~~ 解决方案 这个不用button,用radiobutton. 解决方案二: 我是在RadioGroup中嵌套了一个LinearLayout,这样就可以显示两行了,那么问题来了,radiogroup不能实现单选了,怎么办啊 解决方案三: 我是在RadioGroup中

Android实现类似网易新闻选项卡动态滑动效果_Android

 本文会实现一个类似网易新闻(不说网易新闻大家可能不知道大概是什么样子)点击超多选项卡,选项卡动态滑动的效果. 首先来看看布局,就是用HorizontalScrollView控件来实现滑动的效果,里面包含了一个布局. 接下来我们在onCreat方法中加载布局和构建我们需要显示的数据 <code class="hljs avrasm"> @Override protected void onCreate(Bundle savedInstanceState) { super.o

Android实现类似网易新闻选项卡动态滑动效果

本文会实现一个类似网易新闻(不说网易新闻大家可能不知道大概是什么样子)点击超多选项卡,选项卡动态滑动的效果. 首先来看看布局,就是用HorizontalScrollView控件来实现滑动的效果,里面包含了一个布局. 接下来我们在onCreat方法中加载布局和构建我们需要显示的数据 <code class="hljs avrasm"> @Override protected void onCreate(Bundle savedInstanceState) { super.on

Android利用GridView实现单选效果

1.实现如图所示的单选效果 由于Android提供的单选按钮radiobutton只能单行或单列显示,且样式并不美观,故可用GridView进行改造,实现单选效果,而要实现这样的效果重点就在GridView的适配器这块了. 首先是GridView的item的XML: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical&

Android自定义类似ProgressDialog效果的Dialog

http://blog.csdn.net/qjlhlh/article/details/7979179 Android自定义类似ProgressDialog效果的Dialog. 方法如下: 1.首先准备两张自己要定义成哪样子的效果的图片和背景图片(也可以不要背景). 如我要的效果: 2.定义loading_dialog.xml布局文件(这里你也可以按自己的布局效果定义,关键是要有个imageView): [html] view plaincopy <?xml version="1.0&qu

Android 桌面图标上显示未读消息的图标(类似qq的桌面提示效果)

问题描述 Android 桌面图标上显示未读消息的图标(类似qq的桌面提示效果) 当有新的消息之后,在桌面上提示出来,在网上找了几天,都是只有几个牌子的手机支持,哪位大神有比较全的. 解决方案 Android 类似未读短信图标显示数字效果的分析Android 类似未读短信图标显示数字效果的分析Android 类似未读短信图标显示数字效果的分析 解决方案二: 没研究过,你可以去github上看看 解决方案三: https://github.com/xuyisheng/ShortcutHelper

Android基于ListView实现类似Market分页加载效果示例_Android

本文实例讲述了Android基于ListView实现类似Market分页加载效果.分享给大家供大家参考,具体如下: 最近几天研究ListView实现分页加载和滚动加载,发现可以用listView的OnScroll方法来实现,直接上代码 ListViewScroll.java package zy.lucifer.ListViewScroll; import android.app.Activity; import android.os.Bundle; import android.util.Lo