Android(六) 常用控件(二)单选框,toast,checkbox,progressbar,listview

本文导读:
一共介绍了四个控件:
1.RadioGroup,RadioButton 单选框
2.Toast
3.CheckBox 多选框
4.进度条
5.ListView

RadioGroup/RadioButton

单选框布局


   <RadioGroup
        android:id="@+id/sex"
        android:layout_below="@id/test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton 
            android:id="@+id/male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Male"
            />
        <RadioButton
            android:id="@+id/female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="FeMale" 
            />
    </RadioGroup>

单选框使用

  • 设置监听器
sexRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                if(checkedId == R.id.male)
                {
                    Toast.makeText(ClientMainActivity.this, "male", Toast.LENGTH_SHORT).show();
                }
                else if(checkedId == R.id.female)
                {
                    Toast.makeText(ClientMainActivity.this, "female", Toast.LENGTH_SHORT).show();
                }
            }
   });  

Toast

Toast.makeText(ClientMainActivity.this, "female", Toast.LENGTH_SHORT).show();

如何改变Toast的显示位置?

Toast toast= Toast.makeText(getApplicationContext(),  "Toast Here", Toast.LENGTH_SHORT);  
//放在左上角。如果你想往右边移动,将第二个参数设为>0;往下移动,增大第三个参数;后两个参数都只得像素
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
toast.show();

CheckBox

多选框布局

<CheckBox
        android:layout_below="@id/sex"
        android:id="@+id/swim"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Swim" 
        />
    <CheckBox
        android:layout_below="@id/swim"
        android:id="@+id/jump"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Jump" 
        /> 

多选框使用

  • 单选框和多选框的区别在于,多选框是给各个选项设置监听器
jumpCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked)
                {
                    Toast.makeText(ClientMainActivity.this, "jump", Toast.LENGTH_SHORT).show();
                }
            }
        });

ProgressBar

进度条布局

<ProgressBar

        style="?android:attr/progressBarStyleHorizontal"
        android:max="100"
        android:visibility="gone"
        />
<ProgressBar
        style="?android:attr/progressBarStyle"
        ...
        />  

进度条使用

 firstProgressBar = (ProgressBar) findViewById(R.id.progress1);
 firstProgressBar.setVisibility(View.VISIBLE);//设置为可见
 firstProgressBar.setVisibility(View.GONE);   //设置为不可见
 firstProgressBar.setProgress(50);            //第一进度条
 firstProgressBar.setSecondaryProgress(70);   //第二进度条

ListView

listview的使用一共分为3个部分:
第一个部分,编写lisview布局文件
第二个部分,编写每个条目的布局文件,例如user.xml
第三个部分,设置adapter
记得新建的activity要在mainfest中注册一下

ListView布局

listview的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >  
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/android:list">
    <!--> 记得ID要使用android:list</!-->    
    </ListView>
</LinearLayout>

user的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/user_ip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

ListView的使用

    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", "wang");
    map1.put("user ip", "192.168.1.1");
    map2.put("user name", "wu");
    map2.put("user ip", "192.168.1.2");
    map3.put("user name", "li");
    map3.put("user ip", "192.168.1.3");
    list.add(map1);
    list.add(map2);
    list.add(map3);

    SimpleAdapter adapter = new SimpleAdapter(this, //当前的activity本身
                                                  list, //arraylist
                                                  R.layout.user, //显示条目的布局
                                                  new String[]{"user name", "user ip"}, //从hashmap中得到值得键值对
                                                  new int[]{R.id.user_name, R.id.user_ip});//使用哪个控件来显示

    setListAdapter(adapter);//设置适配器
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猎羽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值