android基础--2

来源:互联网 发布:查看1433端口是否打开 编辑:程序博客网 时间:2024/06/03 17:44
单选框
1.layout中添加
<RadioGroup 
    android:id="@+id/rd"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <RadioButton
    android:id="@+id/rb_male"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/rd"
    android:layout_below="@+id/rd"
    android:layout_marginLeft="22dp"
    android:text="male" />
    <RadioButton
    android:id="@+id/rb_female"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/rd"
    android:layout_below="@+id/rd"
    android:layout_marginLeft="22dp"
    android:text="female" />
    
</RadioGroup>
2.设置监听函数
OnCheckedChangeListener listener =new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean 


isChecked) {
RadioButton rb=(RadioButton) buttonView;

Toast.makeText(MainActivity.this, rb.getText().toString()+"  


"+isChecked, 0).show();
}
};
3.绑定
 RadioButton rb_male=(RadioButton) findViewById(R.id.rb_male);
       RadioButton rb_female=(RadioButton) findViewById(R.id.rb_female);
       rb_male.setOnCheckedChangeListener(listener);
       rb_female.setOnCheckedChangeListener(listener);








Seekbar
1.layout中添加seekbar
2。添加监听器
OnSeekBarChangeListener listener =new OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

System.out.println(seekBar.getProgress());//输出

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
System.out.println(seekBar.getProgress());//


}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
System.out.println(seekBar.getProgress());


}
};








CheckBox复选框
1.在layout中添加控件
2.设置监听器
OnCheckedChangeListener listener =new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean 


isChecked) {
CheckBox cb=(CheckBox) buttonView;
//System.out.println(cb.getText().toString()+"--


>"+isChecked);
Toast.makeText(MainActivity.this, cb.getText().toString()


+"-->"+isChecked, 0).show();

}
};
3。查找关联
   CheckBox cb_fb= (CheckBox) findViewById(R.id.cb_fb);
        CheckBox cb_bb= (CheckBox) findViewById(R.id.cb_bb);
        cb_bb.setOnCheckedChangeListener(listener);
        cb_fb.setOnCheckedChangeListener(listener);








RatingBar评分框
1.layout中添加控件
 <RatingBar
        android:id="@+id/rb"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="91dp" 
        android:numStars="6"
        android:stepSize="0.5"/>
2.OnRatingBarChangeListener listener = new OnRatingBarChangeListener() {

@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
Toast.makeText(MainActivity.this, rating+"", 1).show();

}
};
3.RatingBar rb = (RatingBar) findViewById(R.id.rb);
rb.setOnRatingBarChangeListener(listener);










自动提示框
AutoCompleteTextView
1.在layout中添加控件
2.新建XML文件,并且添加TextView
3.自己建立数据
4.建立适配器
ArrayAdapter adapter =new ArrayAdapter
(MainActivity.this, R.layout.auto,R.id.textView1,data);
//参数1表示当前Activity 2.数据布局位置 3.布局的View 4.数据
5.找到控件
AutoCompleteTextView actv= (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
6.actv.setAdapter(adapter);//绑定适配器








二级列表
ExpandableListView
1.在layout中添加ExpandableListView控件
2.新建父XML以及子XML
3.在父子中添加TextView( android:paddingLeft="40dp"//调节内边距 android:textSize="20sp"//字


体)
4.在src main中添加数据,如下
List<Map<String,String>> groups =new ArrayList<Map<String,String>>();

Map<String,String> group1 =new HashMap<String,String>();
group1.put("group", "group1");

Map<String,String> group2 =new HashMap<String,String>();
group2.put("group", "group2");
groups.add(group1);
groups.add(group2);
//以上建立了组项的数据

List<List<Map<String,String>>> childs = new 


ArrayList<List<Map<String,String>>>();

List<Map<String,String>> child1 =new ArrayList<Map<String,String>>();
List<Map<String,String>> child2 =new ArrayList<Map<String,String>>();

Map<String,String> child1data1 =new HashMap<String,String>();
Map<String,String> child1data2 =new HashMap<String,String>();
Map<String,String> child1data3 =new HashMap<String,String>();
Map<String,String> child2data1 =new HashMap<String,String>();
Map<String,String> child2data2 =new HashMap<String,String>();
Map<String,String> child2data3 =new HashMap<String,String>();
child1data1.put("child", "ls");
child1data2.put("child", "ww");
child1data3.put("child", "js");
child2data1.put("child", "zj");
child2data2.put("child", "hf");
child2data3.put("child", "china");
child1.add(child1data3);
child1.add(child1data2);
child1.add(child1data1);
child2.add(child2data1);
child2.add(child2data2);
child2.add(child2data3);

childs.add(child1);
childs.add(child2);
//子项数据
5.数据适配器的配置
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter
(MainActivity.this, groups, R.layout.father, new String[]{"group"},new int


[]{R.id.tv_father},
childs, R.layout.child, new String[]{"child"}, new int[]


{R.id.tv_father});
/**
* param1:当前对象
* param2:组数据
* param3:组布局文件
* param4:组中map的key数组
* param5:组布局文件中textview的id数组
* param6:子项数据
* param7:子项布局文件
* param8:子项布局文件中map的key数组
* param9:子项布局文件中textview的id数组
*/
6.查找控件并且绑定
ExpandableListView elv =(ExpandableListView) findViewById(R.id.expandableListView1);
elv.setAdapter(adapter);

0 0