标签:android checkbox listview radiobutton 源码
package com.example.impletedemo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.ListView; import android.widget.RadioGroup; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener, CompoundButton.OnCheckedChangeListener, RadioGroup.OnCheckedChangeListener, OnItemLongClickListener, OnItemClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); InitUI(); } private void InitUI() { View Layout1 = findViewById(R.id.Layout1); CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1); CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2); RadioGroup radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1); //接口实现监听,都是需要进行设置监听 if (Layout1 != null) Layout1.setOnClickListener(this); if (checkBox1 != null) checkBox1.setOnCheckedChangeListener((CompoundButton.OnCheckedChangeListener)this); if (checkBox2 != null) checkBox2.setOnCheckedChangeListener((CompoundButton.OnCheckedChangeListener)this); if (radioGroup1 != null) radioGroup1.setOnCheckedChangeListener((RadioGroup.OnCheckedChangeListener)this); ListView myListView = (ListView) findViewById(R.id.listView1); myListView.setOnItemLongClickListener(this); // 设置ListView长按 myListView.setOnItemClickListener(this); // 设置ListView单击 SimpleAdapter simpleAdapter = new SimpleAdapter(this, getDataSource(), R.layout.layout_list_item, new String[]{"title"}, new int[]{R.id.textView1}); myListView.setAdapter(simpleAdapter); } public List<Map<String, Object>> getDataSource() { List<Map<String, Object>> listems = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("title", "北京"); listems.add(map); map = new HashMap<String, Object>(); map.put("title", "上海"); listems.add(map); map = new HashMap<String, Object>(); map.put("title", "广州"); listems.add(map); map = new HashMap<String, Object>(); map.put("title", "南京"); listems.add(map); map = new HashMap<String, Object>(); map.put("title", "苏州"); listems.add(map); return listems; } @Override //一般layout button监听实现方法 public void onClick(View arg0) { if (arg0.getId() == R.id.Layout1) { TextView textView = (TextView) findViewById(R.id.textView1); textView.setText("layout监听"); Toast.makeText(this, "layout监听", Toast.LENGTH_SHORT).show(); } } @Override //checkbox监听实现方法 public void onCheckedChanged(CompoundButton arg0, boolean arg1) { if (arg0.getId() == R.id.checkBox1) { if (arg1) { Toast.makeText(this, "选中单项1", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "取消单项1", Toast.LENGTH_SHORT).show(); } } else if (arg0.getId() == R.id.checkBox2) { if (arg1) { Toast.makeText(this, "选中单项2", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "取消单项2", Toast.LENGTH_SHORT).show(); } } } @Override //RadioGroup监听实现方法 public void onCheckedChanged(RadioGroup arg0, int arg1) { if (arg0.getId() == R.id.radioGroup1) { if (arg1 == R.id.radio1) { Toast.makeText(this, "选中男", Toast.LENGTH_SHORT).show(); }else if (arg1 == R.id.radio2) { Toast.makeText(this, "选中女", Toast.LENGTH_SHORT).show(); } } } @Override //listview长按监听实现方法 public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(this, "listview 长按" + getDataSource().get(arg2), Toast.LENGTH_SHORT).show(); //这里返回一定是true , 不然会同时触发单击 return true; } @Override //listview单击监听实现方法 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(this, "listview 单击" + getDataSource().get(arg2), Toast.LENGTH_SHORT).show(); } }
<LinearLayout xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/Layout1" android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center_vertical" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center_vertical" android:orientation="vertical" > <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="单项1" /> <CheckBox android:id="@+id/checkBox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="单项2" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="90dp" android:gravity="center_vertical" android:orientation="vertical" > <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" /> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" /> </RadioGroup> </LinearLayout> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
标签:android checkbox listview radiobutton 源码
原文地址:http://blog.csdn.net/qq_16064871/article/details/45286119