最近做一个项目,有很多项需要单选,但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