一、多选按钮-CheckBox
用法:首先也是通过控件ID来得到代表控件的对象,然后为其添加监听器。
设置监听器代码:
//为多选按钮添加监听器 swim.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked) { System.out.println("swim is checked"); } else { System.out.println("swim is unchecked"); } } });
其中,swimBox是CompoundButton的子类,所以他可以向上转型为CompoundButton的类型,将参数传进。
二、单选按钮-RadioGroup
用法:同CheckBox
设置监听器代码:
//单选按钮监听器 @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if(female.getId()== checkedId){ System.out.println("你是女的"); Toast.makeText(checkRadio.this, "女", Toast.LENGTH_SHORT).show(); /**Toast用于选择时弹出提示*/ }else if (gender.getId()== checkedId) { System.out.println("你是男的"); Toast.makeText(checkRadio.this, "男", Toast.LENGTH_SHORT).show(); } } });
三、进度条-ProgressBar
.xml文件中定义如下:水平进度条
<ProgressBar android:id="@+id/firstBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="200dp" android:layout_height="wrap_content" android:visibility="gone"/是否可视的状态,gone是不可视的
/>
默认的进度条最大值是100,如上例子,若想进度条自定义,则用:android:max="xxx"即可,也可以在代码里面设置。如:下面例子中!
Activity中实现进度条:
package mars.progressbar; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; public class ProgressBarTest extends Activity { /** Called when the activity is first created. */ //声明变量 private ProgressBar firstBar =null; private ProgressBar secondBar = null; private Button myButton = null; private int i = 0 ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //根据控件的ID来取得代表控件的对象 firstBar = (ProgressBar)findViewById(R.id.firstBar); secondBar = (ProgressBar)findViewById(R.id.secondBar); myButton = (Button)findViewById(R.id.myButton); myButton.setOnClickListener(new ButtonListener()); } class ButtonListener implements OnClickListener{ @Override public void onClick(View v) { if(i == 0) { //设置进度条处于可见的状态 firstBar.setVisibility(View.VISIBLE); firstBar.setMax(150); secondBar.setVisibility(View.VISIBLE); } else if ( i < firstBar.getMax()){//获得获得进度条的最大值 //设置主进度条的当前值 firstBar.setProgress(i); //设置第二进度条的当前值 firstBar.setSecondaryProgress(i + 10); //因为默认的进度条无法显示进行的状态 secondBar.setProgress(i); } else{ //设置进度条处于不可见状态 firstBar.setVisibility(View.GONE); secondBar.setVisibility(View.GONE); } i = i + 10 ; } } }
四、列表-ListView
列表实现的不是Activity,而是ListActivity。
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:id="@+id/listLinearLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawSelectorOnTop="true" android:scrollbars="vertical" /> </LinearLayout> </LinearLayout>
user.xml:
<?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="horizontal" android:paddingLeft="10dip" android:paddingRight="10dip" android:paddingTop="1dip" android:paddingBottom="1dip"> <TextView android:id="@+id/user_name" android:layout_width="180dip" android:layout_height="30dip" android:textSize="5pt" android:singleLine="true" /> <TextView android:id="@+id/user_ip" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="right" android:textSize="5pt" /> </LinearLayout>
Activity代码:
public class Activity01 extends ListActivity { /**继承于ListActivity类*/ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); HashMap<String, String> map1 = new HashMap<String, String>(); HashMap<String, String> map2 = new HashMap<String, String>(); HashMap<String, String> map3 = new HashMap<String, String>(); map1.put("user_name", "zhangsan"); map1.put("user_ip", "192.168.0.1"); map2.put("user_name", "zhangsan"); map2.put("user_ip", "192.168.0.2"); map3.put("user_name", "wangwu"); map3.put("user_ip", "192.168.0.3"); list.add(map1); list.add(map2); list.add(map3); MyAdapter listAdapter = new MyAdapter(this, list,R.layout.user, new String[] { "user_name", "user_ip" }, new int[] { R.id.user_name,R.id.user_ip}); /**this即为当前这个ListActivity的对象, * list关键字,将鼠标放在list上可看到ArrayList<HashMap<String, String>> list * 表示为:首先它是一个ArrayList,里面放上HashMap,HashMap里又放上2个String对象 * R.id.uder_name是布局文件,后面一长串是数组,最后那长串是控制显示在列表中的控件布局 * user_name和user_ip是对应在HashMap里面的列,最后的new int[]则是对应在列中的值*/ setListAdapter(listAdapter); } }MyAdapter是自定义方法,MyAdapter:package mars.listview; import java.util.List; import java.util.Map; import android.R.color; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.SimpleAdapter; public class MyAdapter extends SimpleAdapter { private LayoutInflater inflater = null; private List<Map<String,Object>> styles = null; public List<Map<String, Object>> getStyles() { return styles; } public void setStyles(List<Map<String, Object>> styles) { this.styles = styles; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View result = super.getView(position, convertView, parent); System.out.println("positon---->" + position); if(result != null){ inflater.inflate(R.layout.user1, null); } return result; } public MyAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); inflater = LayoutInflater.from(context); } }
五、SpinnnerSpinner s1 = (Spinner) findViewById(R.id.Sp_Button_ChangeRoom); //下拉列表选择桌位号或者包厢号 //将可选内容与ArrayAdapter连接 ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.SP_ChangeRoom, android.R.layout.simple_spinner_item); //设置下拉列表风格 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //将adapter添加到s1中 s1.setAdapter(adapter); //添加Spinner事件监听 s1.setOnItemSelectedListener(new OnItemSelectedListener() { /** 第一个参数 是适配器 <?>是适配器里内容的类型,可把?改成你存的类型. * 第二个参数 是你当前选择的view * 第三个参数 是你所选项在适配器中的索引位置 * 第四个参数 选定项目的行数 */ public void onItemSelected( AdapterView<?> parent, View view, int position, long id) { if(id>4){ showToast(id+1+"号包厢"); }else{ showToast(id+1+"号桌"); } } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub showToast("没有选择桌号或者包厢号"); } }); } private void showToast(CharSequence msg) { Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();