`
stephen830
  • 浏览: 2977770 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

android中的UI控制(三)

 
阅读更多

android中的UI控制(三)

 

转载自 : http://www.android777.com/index.php/tutorial/android-view/androids-ui-control-c.html

 

上一篇 我 们讲了Android GUI中的MVC架构和一些视图的文字控制能力,接着我们看一些按钮控制的例子。按钮在界面中算是最常见的控件,一般我们都用按钮来触发动作。按钮可以分 为一般按钮(Button)、带有图片的按钮(ImageButton)、有开启关闭状态的按钮(ToggleButton)。

 

Button:

按钮在Android中是android.wiget.Button类,我们常用它来处理一些点击事件。它继承了TextView,所有 TextView可配置的属性都可以在Button上配置,所以我们可以定义很多Button的样式,同时Button还扩展了部分功能让用户可以在文字 的上下左右四个方向添加icon图片。

 

布局文件fourth.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/linerlayout1"
  android:layout_width="fill_parent"
  android:orientation="vertical"
  android:layout_height="fill_parent">
     <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是一个普通按钮"
        android:typeface="serif"
        android:textStyle="bold"
     />
     <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="带图片按钮"
        android:drawableTop="@drawable/icon"
     />
</LinearLayout>

 

对应的效果图如下(图1):

 



 图1

 

我们可以给按钮注册一个点击事件,通过setOnClickListener方法:

     	Button btn1 = (Button)findViewById(R.id.button1);
    	btn1.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				Toast.makeText(MainActivity.this,"Button[button1]点击事件."
						,Toast.LENGTH_SHORT).show();
			}
    	});
 

ImageButton:

ImageButton是一个内容是图片的按钮,它跟Button不一样,Button的内容可以是文字或者文字+图片,但是 ImageButton的内容只能是图片,而且ImageButton是继承ImageView,而不是Button,所以二者是不一样的。你可以通过在 xml布局里设置android:src属性指定显示的图片地址,也可以动态的通过代码调用setImageResource()方法来填充一个图片:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/linerlayout1"
  android:layout_width="fill_parent"
  android:orientation="vertical"
  android:layout_height="fill_parent">
     <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是一个普通按钮"
        android:typeface="serif"
        android:textStyle="bold"
     />
     <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="带图片按钮"
        android:drawableTop="@drawable/icon"
     />
     <ImageButton
	   	android:id="@+id/button3"
	   	android:layout_width="wrap_content"
	   	android:layout_height="wrap_content"
	   	android:src="@drawable/icon"
		/>
</LinearLayout>
 

 

 对应的效果图如下(图2):


图2

 

ToggleButton:

ToggleButton和Checkbox、radio按钮一样是一个有状态的按钮,它有一个开启或关闭状态,在开启状态时,按钮的下面有一条绿 色的粗线,当它是在关闭状态时则粗线变成灰色的。你可以通过配置android:textOn和android:textOff来配置对应两种不同状态时 要显示的文字:

 

	<ToggleButton
	    android:id="@+id/button4"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:textOn="播放"
	    android:textOff="停止"
		/>

   对应的效果图如下(图3):

 


图3

 

CheckBox:

 

这边的CheckBox跟HTML、Java GUI里的Checkbox概念是一样的,它是一个复选框,它也存在两种状态:选中和未选中。用户可以通过调用CheckBox对象的 setChecked()或toggle()方法来改变复选框的状态,通过调用isChecked方法获取选中的值。

可以通过调用CheckBox的setOnCheckedChangeListener()方法来监听Checkbox的状态改变事件。

	<CheckBox
	    android:id="@+id/checkbox1"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="Java"
		/>
	<CheckBox
	    android:id="@+id/checkbox2"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text=".Net"
	    android:checked="true"
		/>
	<CheckBox
	    android:id="@+id/checkbox3"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="C++"
		/>
 

  对应的效果图如下(图4):


图4

RadioButton:

Radio跟html里的radio一样,只允许用户在多个选项里选择其中一个,称为:单选框。为了让多个radio中仅能选择一个,我们需要将这 多个radio放到一个分组中,这样默认每个分组只允许用户选中分组中的一项radio button。所以为了要使用RadioButton首先我们要先创建一个RadioGroup分组,然后将RadioButton放到分组中。

	<RadioGroup
	    android:id="@+id/radiogroup1"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:orientation="horizontal">
	    <RadioButton
	    	android:id="@+id/radiobutton1"
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:text="Java"
	    />
	    <RadioButton
	    	android:id="@+id/radiobutton2"
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:text=".Net"
	    	android:checked="true"
	    />
	    <RadioButton
	    	android:id="@+id/radiobutton3"
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:text="C++"
	    />
	</RadioGroup>

 对应的效果图如下(图5):


图5

所有在RadioGroup中的RadioButton默认都是未选中状态,但是你也可以通过xml布局,设置某个RadioButton处于选中 状态。跟CheckBox一样,你可以调用setChecked()和toggle()方法来改变它里面的状态值。也可以监听它的状态改变事件。

 

 

在RadioGroup中你也可以存放其他不是RadioButton的控件,当然通常你不会那么做,因为那只会在界面上混淆用户,不过也不会改变 RadioGroup和RadioButton的行为。当用户选中一个RadioButton时,他再次点击RadioButton也不会改变 RadioButton状态,那如果整个RadioGroup要处于未选状态时要怎么处理呢?这边可以调用RadioGroup中的 clearCheck()方法,它会将内部所有RadioButton状态改为未选中状态。

 

 

 

 

  • 大小: 15.1 KB
  • 大小: 17.7 KB
  • 大小: 18.7 KB
  • 大小: 23.1 KB
  • 大小: 26.9 KB
分享到:
评论

相关推荐

    Android新手UI集合全

    在实际开发中,Android Studio提供了强大的设计工具,如Layout Editor,可以可视化地设计和预览界面,同时支持XML代码编写,方便开发者进行精确控制。此外,Android Studio还提供了Vector Asset Studio,可以将SVG...

    原生android SystemUI源码

    在Android 4.0(Ice Cream Sandwich,简称ICS)版本中,SystemUI组件负责了状态栏、通知中心、快速设置面板等用户界面元素的显示与交互。 SystemUI主要包含以下几个关键组件: 1. **StatusBar**: 状态栏是Android...

    Android 智能UI锁屏

    在Android系统中,智能UI锁屏是指一种个性化且功能丰富的屏幕锁定界面,它不仅提供基本的解锁功能,还集成了各种实用工具和个性化设置,以提升用户体验。这种锁屏技术通常涉及用户界面设计、交互逻辑、安全性以及...

    androidUI设计器

    Android UI 设计器是Android开发中的一个重要工具,它允许开发者直观地设计应用程序的用户界面,无需手动编写XML布局代码。这个工具极大地提升了开发效率,使得非程序员也能参与到UI设计中来,实现快速预览和调整...

    Android所有UI控件

    在Android开发中,UI(用户界面)控件是构建应用程序不可或缺的部分。它们为用户提供与应用交互的方式,使得信息展示和操作更加直观。Android系统提供了多种控件,涵盖各种功能,从简单的按钮到复杂的布局。本篇文章...

    安卓Android源码——ui开发类库示例源码.zip

    9. **Third-party UI库**:除了官方组件,还有很多第三方库增强了Android的UI开发能力,如Butter Knife(视图注入)、Retrofit(网络请求)、Glide(图片加载)、Picasso(图片加载)等。这些库简化了开发过程,提高...

    Android things简单的UI

    **UI在Android Things中的应用** 在Android Things上实现UI,主要是通过使用Android的View系统,这个系统包括各种组件,如Button、TextView、ImageView等,它们可以用来构建用户界面。虽然Android Things的硬件限制...

    android UI 各种小例子

    在Android开发中,UI设计是至关重要的一环,它关乎到应用程序的用户体验和视觉吸引力。本压缩包中的"android UI 各种小例子"为初学者提供了丰富的实践资源,旨在帮助开发者掌握基本的Android用户界面设计技巧。每个...

    android 小米UI 自定义音量

    在Android中,音量控制通常使用滑动条或者进度条来表示音量大小,因此CircleProgressView可能是小米UI自定义音量控件的核心组件,它可能是一个圆形的进度条,提供更美观、直观的音量调节体验。 要移植小米UI的...

    android各种UI特效 工作中积累

    在Android开发中,UI特效是提升用户体验的关键因素之一。它涉及到界面的动态效果、过渡动画、自定义控件以及交互设计等多个方面。本资源“工作中积累UI特效”显然是一个开发者在实际工作过程中积累的Android UI设计...

    android 仿锤子UI布局

    在Android中,布局(Layout)是组织和控制View组件的基础。常见的布局有LinearLayout(线性布局)、RelativeLayout(相对布局)、FrameLayout(帧布局)和GridLayout(网格布局)。锤子UI的格子布局通常结合了...

    移植Android的UI组件到Web端以Android的方式来制作高性能优体验的WebApp

    2. **自定义组件系统**:AndroidUI4Web可能有一个自定义的组件库,包含各种Android原生UI元素的Web实现,如按钮、列表视图、滑动选择器等,这些组件可以方便地通过JavaScript进行控制和定制。 3. **手势识别**:...

    android项目整体UI框架

    "android项目整体UI框架"这个主题主要涵盖了三个方面:自定义Android项目的底部任务栏、实用Fragment的切换以及项目整体架构的设计。下面将详细讨论这些知识点。 首先,我们来看自定义Android项目底部任务栏。底部...

    android ui旋转 控制系统旋转

    支持全志平台的ui旋转,里面有全志平台的系统签名,通过反射 IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); wm.freezeRotation(value); 实现ui旋转,其他平台需要系统签名后使用!切记必须要...

    android 7.0 SystemUI

    在Android系统架构中,SystemUI位于应用层与系统服务层之间,它提供了通知中心、状态栏、快速设置面板、锁屏界面以及各种系统级别的交互控件。深入理解SystemUI的源代码对于开发者优化系统UI性能、自定义系统行为或...

    android-ui-master

    在Android开发中,UI设计和交互是至关重要的部分,尤其是对于用户操作反馈的动画效果。"android-ui-master"这个项目显然关注的是Android平台上的界面元素动画,特别是返回按钮的旋转和按钮转动效果。这些动画可以...

    Android UI 基础教程

    在"Android UI 基础教程"中,你将逐步学习这些知识点,并通过实例操作加深理解。通过阅读这份教程,你将具备独立设计和实现Android用户界面的能力,为后续的进阶学习打下坚实基础。记得实践是检验理论的最好方法,...

    android验证码界面ui实现

    在Android应用开发中,验证码界面UI的实现是一个常见的功能,主要用于身份验证或安全验证环节。本文将详细讲解如何在Android中实现一个具有60秒倒计时功能的验证码界面,以及如何通过PopupWindow来弹出这个界面。 ...

    android UI定制的一些资料

    在Android平台上,UI(用户界面)定制是一项关键的技术任务,它允许开发者根据需求和品牌风格创建独特的用户体验。这里,我们探讨的是"android UI定制的一些资料",这些资料可能包括设计原则、布局技巧、自定义视图...

    Android8.1 SystemUI源码

    在Android系统中,SystemUI是用户界面的核心组成部分,它负责管理状态栏、通知中心、快速设置等关键功能。本文将深入探讨Android 8.1版本的SystemUI源码,介绍其结构、工作原理以及如何利用提供的gradle配置进行开发...

Global site tag (gtag.js) - Google Analytics