码迷,mamicode.com
首页 > 移动开发 > 详细

Android控件介绍

时间:2015-07-22 22:54:32      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:

Android控件介绍

多选按钮(CheckBox)

CheckBox有两个常用的事件,OnClickListener事件和OnClickChangeListener事件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <CheckBox
        android:id="@+id/eat_checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Eat"/>
    <CheckBox
        android:id="@+id/sleep_checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/eat_checkBox"
        android:text="Sleep"/>


</RelativeLayout>

效果如下:

技术分享

代码:

package com.example.z1178.test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;


public class MainActivity extends Activity {

    private static final String TAG="debug";
    private CheckBox eat_checkBox;
    private CheckBox sleep_checkBox;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.linear_layout);


        eat_checkBox=(CheckBox)findViewById(R.id.eat_checkBox);
        sleep_checkBox=(CheckBox)findViewById(R.id.sleep_checkBox);

        OnCheckBoxClickListener  listener1=new OnCheckBoxClickListener();
        CompoundButton.OnCheckedChangeListener listener2=new OnCheckedChangeListener();
        //绑定点击事件
        eat_checkBox.setOnClickListener(listener1);
        sleep_checkBox.setOnClickListener(listener1);
        //绑定状态改变事件
        eat_checkBox.setOnCheckedChangeListener(listener2);
        sleep_checkBox.setOnCheckedChangeListener(listener2);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    class OnCheckBoxClickListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            CheckBox box=(CheckBox)v;
            int id=v.getId();
            switch (id){
                case R.id.eat_checkBox:Log.d(TAG,"eat_checkBox is clicked!:" + box.isChecked());break;
                case R.id.sleep_checkBox:Log.d(TAG, "sleep_checkBox is clicked:" + box.isChecked());break;
            }
        }
    }

    class OnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            CheckBox box=(CheckBox)buttonView;
            Log.d(TAG,box.getText()+" checkBox changed ,its statue is "+isChecked);

        }
    }

}

若要在界面上增加全选按钮,则先在xml中增加相应的控件,然后在代码中获取该对象,然后绑定OnClickChangeListener事件,关键代码如下:

  @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            CheckBox box=(CheckBox)buttonView;
            int id=box.getId();
            if(id!=R.id.checkall_checkBox){
                Log.d(TAG,box.getText()+" checkBox changed ,its statue is "+isChecked);
            }else{
                    eat_checkBox.setChecked(isChecked);
                    sleep_checkBox.setChecked(isChecked);
            }


        }

单选按钮(RadioButton)

单选按钮有RadioButton和RadioGroup,其事件与CheckBox一样,也有OnClickListener事件和OnClickChangeListener事件,不再赘述。

ImageView

配置控件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:id="@+id/imageView"
        android:layout_height="wrap_content"
        android:src="@drawable/layout_image"
        />

</RelativeLayout>

我们也可以通过在代码中设置图片

imageView.setImageResource(R.drawable.layout_image);

imageView为ImageView对象,R.drawable.layout_image为drawable中的图片

ScaleType

ScaleType可以控制图片的相关属性,其属性如下:

属性
CENTER
CENTER_CROP
CENTER_INSIDE
FIT_CENTER(START,END)
FITXY
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#ff0000"
        android:scaleType="fitCenter"
        android:src="@drawable/layout_image"
        />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/beauty"
        android:background="#0000ff"
        android:scaleType="fitCenter"
        android:layout_below="@+id/imageView1"
        />

</RelativeLayout>

效果如图:

技术分享

其中,我们设置了图片的width和height为100dp,scaleType为fitCenter,同时设置了背景色,图片会按照width和height等比例缩放,不够的地方背景色填充。

若其中一个的scaleType为fitStart时,效果如下:

技术分享

scaleType为center时:

技术分享

此时图片大于规定的尺寸,则进行裁剪,小于规定尺寸的则居中显示,注意此时无背景色了。

scaleType为centerInside时:

技术分享

centerInside与centerFit的区别是,当图片过大时,两者没有区别,但是当图片小于规定尺寸时,centerFit会进行放大以适应尺寸,而centerInside不会这样做,只是放在中央。

scaleType为centerCrop时:

技术分享

centerCrop为图片的短边于尺寸短边按比例缩放,长边过长则截取掉,因此其是没有背景色的。

当然,我们也可以在代码中实现图片的缩放。

imageView.setScaleType(ScaleType.CENTER);

其参数为枚举类型。

TimePicker和DatePicker

TimePicker和DatePicker均有OnTimeChangedListener事件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TimePicker
        android:id="@+id/timePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </TimePicker>

    <DatePicker
        android:id="@+id/datePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/timePicker"
        >


    </DatePicker>
</RelativeLayout>
package com.example.z1178.test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TimePicker;


public class MainActivity extends Activity {

    private static final String TAG="debug";
    private TimePicker timePicker;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.linear_layout);

        timePicker=(TimePicker)findViewById(R.id.timePicker);
        timePicker.setIs24HourView(true);

        //设置监听器和绑定事件
        timePickerChangedListener listener=new timePickerChangedListener();
        timePicker.setOnTimeChangedListener(listener);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    class timePickerChangedListener implements TimePicker.OnTimeChangedListener{

        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            Log.d(TAG,hourOfDay+":"+minute);
        }
    }

}



默认情况下,时间显示的是当前系统时间,我们可以在代码中设置时间,还有,我们可以通过页面上的按钮设置监听器来获得时间。注意,月份是从0开始的 。

AnalogClock和DigitalClock这里就不介绍了。

ProgressBar,SeekBar和RatingBar

ProgressBar的属性:

属性1 属性2 方法
水平风格 (Horizontal) 判断是否是圆形进度条(isIndeterminated) 最大进度(max)
小风格 (Samll) 增加进度(incrementProgressBy) 当前进度(progress)
大风格 (Large ) 增加第二进度(incrementSecondProgressBy) 第二进度(secondProgress)
反向风格 (Large.Inverse)
大反向风格 (Inverse)
小反向风格 (Samll.Inverse)

SeekBar的监听事件

事件
onProgressChanged(SeekBar seekBar,int progress,boolean fromUser)
onStartTrackingTouch(SeekBar seekBar)
onStopTrackingTouch(SeekBar seekBar)

RatingBar的属性:

属性 事件
星星个数(numStars) onRatingBarChanged(RatingBar ratingBar,float rating,boolean fromUser)
当前等级(progress)
stepSize

版权声明:本文为博主原创文章,未经博主允许不得转载。

Android控件介绍

标签:

原文地址:http://blog.csdn.net/u010999240/article/details/46992143

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!