安卓UI十大基本控件

前面看了TextView控件,后面不想看了,先把控件都记一下,知道功能,后面用到了再查资料吧

1、EditText(输入框)                                                      用于输入文本

2、 Button(按钮)与ImageButton(图像按钮)        用于点击事件

3、ImageView(图像视图)                                  用于 玩图片的,可以设置图片填满,图纵横比,圆角图之类

4、RadioButton(单选按钮)&Checkbox(复选框)  

单选框布局代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择性别"
        android:textSize="23dp"
        />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/btnMan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:checked="true"/>

        <RadioButton
            android:id="@+id/btnWoman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"/>
    </RadioGroup>

    <Button
        android:id="@+id/btnpost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"/>

</LinearLayout>



点击事件判断:


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup);
//        //第一种获得单选按钮值的方法  一开始是男,选了女之后会有提示,而不需要点击提交
//        //为radioGroup设置一个监听器:setOnCheckedChanged()
//        radgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(RadioGroup group, int checkedId) {
//                RadioButton radbtn = (RadioButton) findViewById(checkedId);
//                Toast.makeText(getApplicationContext(), "按钮组值发生改变,你选了" + radbtn.getText(), Toast.LENGTH_LONG).show();
//            }
//        });
        //////// 第二种方式,通过点击提交按钮获取值
        Button btnchange = (Button) findViewById(R.id.btnpost);
        RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup);
        //为radioGroup设置一个监听器:setOnCheckedChanged()
        btnchange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < radgroup.getChildCount(); i++) {//getChildCount( )获得按钮组中的单选按钮的数目;
                    RadioButton rd = (RadioButton) radgroup.getChildAt(i);//getChinldAt(i):根据索引值获取我们的单选按钮
                    if (rd.isChecked()) {   //isChecked( ):判断按钮是否选中
                        Toast.makeText(getApplicationContext(), "点击提交按钮,获取你选择的是:" + rd.getText(), Toast.LENGTH_LONG).show();
                        break;
                    }
                }
            }
        });
    }
复选框布局类似,大差不差,同时,可以根据需求修改选择框样式




5、开关按钮ToggleButton和开关Switch

6、ProgressBar(进度条)

  • android:max:进度条的最大值
  • android:progress:进度条已完成进度值
  • android:progressDrawable:设置轨道对应的Drawable对象
  • android:indeterminate:如果设置成true,则进度条不精确显示进度
  • android:indeterminateDrawable:设置不显示进度的进度条的Drawable对象
  • android:indeterminateDuration:设置不精确显示进度的持续时间
  • android:secondaryProgress:二级进度条,类似于视频播放的一条是当前播放进度,一条是缓冲进度,前者通过progress属性进行设置!


7、SeekBar(拖动条)                                        这玩意是进度条的子类

android:max="100" //滑动条的最大值

android:progress="60" //滑动条的当前值

android:secondaryProgress="70" //二级滑动条的进度

android:thumb = "@mipmap/sb_icon" //滑块的drawable

接着要说下SeekBar的事件了,SeekBar.OnSeekBarChangeListener 我们只需重写三个对应的方法:

onProgressChanged:进度发生改变时会触发

onStartTrackingTouch:按住SeekBar时会触发

onStopTrackingTouch:放开SeekBar时触发

8、RatingBar(星级评分条)   最傻逼和最简单的。。。。。

9、ScrollView(滚动条)

滚动到底部:

我们可以直接利用ScrollView给我们提供的:fullScroll()方法

scrollView.fullScroll(ScrollView.FOCUS_DOWN);滚动到底部

scrollView.fullScroll(ScrollView.FOCUS_UP);滚动到顶部

另外用这玩意的时候要小心异步的玩意,就是addView后,有可能还没有显示完, 如果这个时候直接调用该方法的话,可能会无效,这就需要自己写handler来更新了~


public static void scrollToBottom(final View scroll, final View inner) {

    Handler mHandler = new Handler();
    mHandler.post(new Runnable() {
        public void run() {
            if (scroll == null || inner == null) {
                return;
            }
            int offset = inner.getMeasuredHeight() - scroll.getHeight();
            if (offset < 0) {
                offset = 0;
            }
            scroll.scrollTo(0, offset);
        }
    });
}  

2.设置滚动的滑块图片

这个更加简单: 垂直方向滑块:android:scrollbarThumbVertical
水平方向滑块:android:scrollbarThumbHorizontal





发表评论 / Comment

用心评论~