Android基础控件

 

5.1android开发中,在实现不同的功能以及显示不同的界面效果,那么我们就需要使用控件。之前也介绍过三个基础控件,本章将继续介绍一些基础控件。

5.2 单选按钮、复选框控件

这两个控件就是提供给用户进行选择的时候一种好的体验:比如有时候不需要用户亲自输入,那么我们就提供给用户操作更快捷的选项。单选按钮(RadioButton)就是在这个选项中,用户只能选择一个选项。而复选框(CheckBox)控件顾名思义就是可以选择多个选项。下面就介绍这两个控件。

5.2.1示例:

示例一:RadioButton控件的用法(这里采用布局文件方法来演示,先说明RadioButton的用法):

<?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="vertical" >

<!--这里是定义了一组RadioButton,然后分为三个选项-->

    <RadioButton

        android:id="@+id/first_radiobutton"

        android:checked="true"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:onClick="onAction"

        android:text="" />

    <RadioButton

        android:id="@+id/second_radiobutton"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:onClick="onAction"

        android:text="" />

    <RadioButton

        android:id="@+id/third_radiobutton"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:onClick="onAction"

        android:text="其他" />

</LinearLayout>

这上面是给出的布局文件的代码,这里主要是实现图5.1的布局界面,而真正要实现功能的代码不在这块。上述代码中的:android:checked="true"; 是用来设置默认选中的那个选项,而android:onClick="onAction"是设置监听事件方法,在java代码中实现。这里可以使用RadioGroup要定义一组按钮,也就是说,在这一个组内,选项有用。这里采用java代码实现。代码如下:

package xbb.bzq.android.app;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.RadioButton;

public class RadioButtonAndCheckBoxTestActivity extends Activity {

//定义三个RadioButton变量

private RadioButton mButton1, mButton2, mButton3;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//实例化三个单选按钮控件

mButton1 =

 (RadioButton) findViewById(R.id.first_radiobutton);

mButton2 =

 (RadioButton) findViewById(R.id.second_radiobutton);

mButton3 =

 (RadioButton) findViewById(R.id.third_radiobutton);

}

   /**

    * 这是实现的监听方法,主要是实现修改选项的值

    * @param v

    */

public void onAction(View v) {

//通过获取id来判断用户的选择,然后改变控件的选择状态

switch (v.getId()) {

case R.id.first_radiobutton:

mButton2.setChecked(false);

mButton3.setChecked(false);

mButton1.setChecked(true);

break;

case R.id.second_radiobutton:

mButton1.setChecked(false);

mButton3.setChecked(false);

mButton2.setChecked(true);

break;

case R.id.third_radiobutton:

mButton2.setChecked(false);

mButton1.setChecked(false);

mButton3.setChecked(true);

break;

}

}

}

上面就实现了选择有效,也就是只能选择一个选项的实例,其效果如图5.1和图5.2所示:

 

  

 图 5.1 演示RadioButton控件的用法       图5.2 选择改变后的显示界面

 

示例二:CheckBox控件的用法

 <CheckBox 

        android:id="@+id/first_checkbox"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:onClick="onAction"

        android:text="打篮球"

        />

    

    <CheckBox 

        android:id="@+id/second_checkbox"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:onClick="onAction"

        android:text="踢足球"

        />

    

    <CheckBox 

        android:id="@+id/third_checkbox"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:onClick="onAction"

        android:text="打羽毛球"

        />

    

    <CheckBox 

        android:id="@+id/forth_checkbox"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:onClick="onAction"

        android:text="其它"

        />

这之上只是添加了四个CheckBox控件,也就是说给用户提供了四个选项,这是复选框,可以进行多项选择。然后,这里在java代码中演示一个获取复选框中的值,代码如下:

/**

    * 这是实现的监听方法,主要是实现修改选项的值

    * @param v

    */

public void onAction(View v) {

//这里是获取第一个CheckBox

CheckBox mCheckBox

=(CheckBox)findViewById(R.id.first_checkbox);

//这里是后去第一个CheckBox上的值

String title=mCheckBox.getText().toString();

//这是弹出一个Toast,显示获取到的值

Toast.makeText(this, "你选择的是:"

+title, Toast.LENGTH_LONG).show();

}

这里是实现的一个监听事件,然后这里演示了一个获取CheckBox上的值。然后这里回顾了Toast信息栏。上面显示的效果如图5.3

 

图 5.3 CheckBox界面显示效果

 

5.3  进度条(ProgressBar)、拖动条(SeekBar

进度条是Android UI界面显示的又一个非常实用的控件。通常向用户显示某个耗时操作完成的百分比,因此,进度条可以动态的显示进度。

SeekBar多用于音乐播放以及视屏播放等处,具体实现如下:

<?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="vertical" >

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/hello" />

    <!--这是定义的一个进度条控件,默认设置style是水平显示  -->

    <ProgressBar

        android:id="@+id/progerssbar"

        style="@android:style/Widget.ProgressBar.Horizontal"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:max="1000"

        android:progress="450"

         />

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="上面显示的是ProgressBar、下面显示的是SeekBar" />

    <SeekBar 

        android:id="@+id/seekBar"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        />

</LinearLayout>

 

在上述代码中:style="@android:style/Widget.ProgressBar.Horizontal"

是改变进度条的显示风格,其有很多种显示风格,这里就不一一介绍了,主要参考Android官方文档。然后max是设置进度条的最大值,progress是设置默认值。显示效果图如图5.4所示。这里也很明显看出进度条和拖动条的不同:一个不能手动操作,一个可以手动操作。

 

图 5.4 进度条与拖动条显示效果

实例三:警告对话框的实现(alertDialog

alertDialog就是一个与用户交互的活动窗口,这个对话框里面提供可选的按钮来让用户做出选择,然后进行操作!

1.通过alertDialog的静态内部类Builder来实例化一个对象,由该类来构造我们的对话框

2):通过Builder实例haunted的对象来设置对话框的标题、按钮、以及响应事件

3):调用Builder对象的create()方法来创建这个对话框

4):最后调用builder对象的show()方法来显示我们的对话框

package cn.hong;

 

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.View;

 

public class DialogActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

    }

 

public void onAction(View v){

createAlertDialog();

}

public void createAlertDialog(){

//通过builder来创建一个builder对象,其中this表示当前的上下文

AlertDialog.Builder builder=new AlertDialog.Builder(this);

//得到标题

builder.setTitle("hello");

//设置信息

builder.setMessage("Do you want to cancel it?");

//设置button

builder.setPositiveButton("Sure", new DialogInterface.OnClickListener() {

 

@Override

public void onClick(DialogInterface dialog, int which) {

finish();

 

}

});

builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {

 

@Override

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

 

}

});

builder.create().show();

 

}

}

 

 

实例四:进度对话框(progressDialog

android里面,系统为我们提供了一个类:ProgressDialog,这个类方便我们在后台处理一些耗时的操作来与我们用户进行实时交互,下面我们就模拟一个场景来讲解:

1):创建一个类来继承Activity

2):通过布局实例化一个Button,并给这个Button注册一个单机事件

3):创建一个后台工作线程,模拟软件的安装进度,创建进度条对话框代码如下:

package cn.hong;

import android.app.Activity;

import android.app.ProgressDialog;

import android.os.Bundle;

import android.view.View;

public class ProgressActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

    }

    public void onAction(View v){

     final ProgressDialog progressDialog=new ProgressDialog(ProgressActivity.this);

     createProgressDialog(progressDialog);

     //创建一个工作线程来模拟软件安装

     new Thread(new Runnable() {

int currentProgress=0;

@Override

public void run() {

// TODO Auto-generated method stub

try {

while (true) {

//更新当前进度

progressDialog.setProgress(currentProgress);

currentProgress++;

if(currentProgress>10){

break;

}

//通过休眠时间来模拟软件安装所消耗的时间

Thread.sleep(300);

}

} catch (Exception e) {

// TODO: handle exception

}finally{

//安装结束

progressDialog.dismiss();

}

}

}).start();

    }

private void createProgressDialog(ProgressDialog progressDialog) {

//设置标题

progressDialog.setTitle("安装QQ");

//设置消息内容

progressDialog.setMessage("正在解压文件,请稍后。。。");

//调用show方法来显示进度对话框

// ProgressDialog.STYLE_HORIZONTAL=BIND_AUTO_CREATE;

progressDialog.setProgress(0);

progressDialog.setMax(10);

progressDialog.show();

}

}

 

 

 

 

 

 

 

一切只是为了充实自己!!stay hungry and stay foolish!!
原文地址:https://www.cnblogs.com/Catherine-Brain/p/3290628.html