`

系出名门Android(5) – 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox

 
阅读更多

原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处 、作者信息和本声明。否则将追究法律责任。http://webabcd.blog.51cto.com/1787395/342055

[索引页]


[源码下载]

点击链接下载Android
大小 : 1.95 MB
下载次数 : 0

系出名门Android(5) – 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox, RadioButton, AnalogClock, DigitalClock

作者:webabcd

介绍
在 Android 中使用各种控件(View)

  • TextView- 文本显示控件
  • Button- 按钮控件
  • ImageButton- 图片按钮控件
  • ImageView- 图片显示控件
  • CheckBox- 复选框控件
  • RadioButton- 单选框控件
  • AnalogClock- 钟表(带表盘的那种)控件
  • DigitalClock- 电子表控件

1、TextView 的 Demo
textview.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 

        <!-- 
                TextView - 文本显示控件 
        --> 
        <TextView android:layout_width="fill_parent" 
                android:layout_height="wrap_content" android:id="@+id/textView" /> 

</LinearLayout>

_TextView.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.webabcd.view; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 

public class _TextView extends Activity { 

        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
                // TODO Auto-generated method stub 
                super.onCreate(savedInstanceState); 
                this.setContentView(R.layout.textview); 

                // 设置 Activity 的标题 
                setTitle("TextView"); 

                TextView txt = (TextView) this.findViewById(R.id.textView); 
                // 设置文本显示控件的文本内容,需要换行的话就用“/n” 
                txt.setText("我是 TextView/n显示文字用的"); 
        } 
}

2、Button 的 Demo
button.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 

        <TextView android:layout_width="fill_parent" 
                android:layout_height="wrap_content" android:id="@+id/textView" /> 

         <!-- 
                 Button - 按钮控件 
         -->         
        <Button android:id="@+id/button" 
                android:layout_width="wrap_content" android:layout_height="wrap_content"> 
        </Button> 

</LinearLayout>

_Button.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.webabcd.view; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class _Button extends Activity { 

        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
                // TODO Auto-generated method stub 
                super.onCreate(savedInstanceState); 
                this.setContentView(R.layout.button); 

                setTitle("Button"); 

                Button btn = (Button) this.findViewById(R.id.button); 
                btn.setText("click me"); 

                // setOnClickListener() - 响应按钮的鼠标单击事件 
                btn.setOnClickListener(new Button.OnClickListener(){ 
                        @Override 
                        public void onClick(View v) { 
                                TextView txt = (TextView) _Button.this.findViewById(R.id.textView); 
                                txt.setText("按钮被单击了"); 
                        } 
                }); 
        } 
}

3、ImageButton 的 Demo
imagebutton.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 

        <TextView android:layout_width="fill_parent" 
                android:layout_height="wrap_content" android:id="@+id/textView" /> 

        <!-- 
                ImageButton - 图片按钮控件 
        -->         
        <ImageButton android:id="@+id/imageButton" 
                android:layout_width="wrap_content" android:layout_height="wrap_content"> 
        </ImageButton> 

</LinearLayout>

_ImageButton.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.webabcd.view; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageButton; 
import android.widget.TextView; 

public class _ImageButton extends Activity { 

        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
                // TODO Auto-generated method stub 
                super.onCreate(savedInstanceState); 
                this.setContentView(R.layout.imagebutton); 

                setTitle("ImageButton"); 

                ImageButton imgButton = (ImageButton) this.findViewById(R.id.imageButton); 
                // 设置图片按钮的背景 
                imgButton.setBackgroundResource(R.drawable.icon01); 

                // setOnClickListener() - 响应图片按钮的鼠标单击事件 
                imgButton.setOnClickListener(new Button.OnClickListener(){ 
                        @Override 
                        public void onClick(View v) { 
                                TextView txt = (TextView) _ImageButton.this.findViewById(R.id.textView); 
                                txt.setText("图片按钮被单击了"); 
                        } 
                }); 
        } 
}

4、ImageView 的 Demo
imageview.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 

        <!-- 
                ImageView - 图片显示控件 
        --> 
        <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" 
                android:layout_height="wrap_content"></ImageView> 

</LinearLayout>

_ImageView.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.webabcd.view; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.ImageView; 

public class _ImageView extends Activity { 

        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
                // TODO Auto-generated method stub 
                super.onCreate(savedInstanceState); 
                this.setContentView(R.layout.imageview); 

                setTitle("ImageView"); 

                ImageView imgView = (ImageView) this.findViewById(R.id.imageView); 
                // 指定需要显示的图片 
                imgView.setBackgroundResource(R.drawable.icon01); 
        } 
}

5、CheckBox 的 Demo
checkbox.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 

        <TextView android:layout_width="fill_parent" 
                android:layout_height="wrap_content" android:id="@+id/textView" /> 

        <!-- 
                CheckBox - 复选框控件 
        --> 
        <CheckBox android:text="CheckBox01" android:id="@+id/chk1" 
                android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
        <CheckBox android:text="CheckBox02" android:id="@+id/chk2" 
                android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
        <CheckBox android:text="CheckBox03" android:id="@+id/chk3" 
                android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 

</LinearLayout>

_CheckBox.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.webabcd.view; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.TextView; 

public class _CheckBox extends Activity { 

        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
                // TODO Auto-generated method stub 
                super.onCreate(savedInstanceState); 
                this.setContentView(R.layout.checkbox); 

                setTitle("CheckBox"); 

                CheckBox chk = (CheckBox) this.findViewById(R.id.chk1); 
                // setOnCheckedChangeListener() - 响应复选框的选中状态改变事件 
                chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
                        @Override 
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
                                TextView txt = (TextView) _CheckBox.this.findViewById(R.id.textView); 
                                txt.setText("CheckBox01 的选中状态:" + String.valueOf(isChecked));                                 
                        } 
                }); 
        } 
}

6、RadioButton 的 Demo
radiobutton.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 

        <TextView android:layout_width="fill_parent" 
                android:layout_height="wrap_content" android:id="@+id/textView" /> 

        <!-- 
                RadioButton - 单选框控件 
                RadioGroup - 对其内的单选框控件做分组 
                        checkedButton - 指定组内被选中的单选框的 ID 
        --> 
        <RadioGroup android:id="@+id/radioGroup" 
                android:layout_width="fill_parent" android:layout_height="fill_parent" 
                android:checkedButton="@+id/rad3" android:orientation="horizontal" 
                android:gravity="center_vertical|center_horizontal"> 
                <RadioButton android:text="rad1" android:id="@+id/rad1" 
                        android:layout_width="wrap_content" android:layout_height="wrap_content"></RadioButton> 
                <RadioButton android:text="rad2" android:id="@+id/rad2" 
                        android:layout_width="wrap_content" android:layout_height="wrap_content"></RadioButton> 
                <RadioButton android:text="rad3" android:id="@+id/rad3" 
                        android:layout_width="wrap_content" android:layout_height="wrap_content"></RadioButton> 
        </RadioGroup> 

</LinearLayout>

_RadioButton.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.webabcd.view; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 

public class _RadioButton extends Activity { 

        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
                // TODO Auto-generated method stub 
                super.onCreate(savedInstanceState); 
                this.setContentView(R.layout.radiobutton); 

                setTitle("RadioButton"); 

                RadioGroup group = (RadioGroup) this.findViewById(R.id.radioGroup); 
                // setOnCheckedChangeListener() - 响应单选框组内的选中项发生变化时的事件 
                group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {         
                        @Override 
                        public void onCheckedChanged(RadioGroup group, int checkedId) { 
                                TextView txt = (TextView) _RadioButton.this.findViewById(R.id.textView); 
                                txt.setText(((RadioButton)findViewById(checkedId)).getText() + " 被选中");                                         
                        } 
                });    
        } 
}

7、AnalogClock 的 Demo
analogclock.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 

        <!-- 
                AnalogClock - 钟表(带表盘的那种)控件 
        --> 
        <AnalogClock android:id="@+id/analogClock" 
                android:layout_width="wrap_content" android:layout_height="wrap_content"> 
        </AnalogClock> 

</LinearLayout>

_AnalogClock.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.webabcd.view; 

import android.app.Activity; 
import android.os.Bundle; 

public class _AnalogClock extends Activity { 

        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
                // TODO Auto-generated method stub 
                super.onCreate(savedInstanceState); 
                this.setContentView(R.layout.analogclcok); 

                setTitle("AnalogClock"); 
        } 
}

8、DigitalClock 的 Demo
digitalclock.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 

        <!-- 
                DigitalClock - 电子表控件 
        --> 
        <DigitalClock android:id="@+id/digitalClock" 
                android:layout_width="wrap_content" android:layout_height="wrap_content"> 
        </DigitalClock> 

</LinearLayout>

_DigitalClock.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.webabcd.view; 

import android.app.Activity; 
import android.os.Bundle; 

public class _DigitalClock extends Activity { 

        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
                // TODO Auto-generated method stub 
                super.onCreate(savedInstanceState); 
                this.setContentView(R.layout.digitalclcok); 

                setTitle("DigitalClcok"); 
        } 
}

OK

本文出自 “webabcd” 博客,请务必保留此出处http://webabcd.blog.51cto.com/1787395/342055

转载编辑: flysolo
转载地址:http://disanji.net/2010/12/26/famous-android-5-view-textview-button-imagebutton-imageview-checkbox/
分享到:
评论

相关推荐

    实验二-Android基本控件应用.docx

    按钮控件是 Android 中最常用的控件之一,包括 Button 和 ImageButton 两种。Button 控件继承自 TextView 类,用于实现简单的按钮功能。ImageButton 控件继承自 ImageView,用于显示图片按钮。 状态开关按钮...

    Android控件大全以及各布局控件的使用方式

    ### Android控件大全及各布局控件的使用详解 #### 一、Android控件概述 Android控件是指在Android应用程序中用于实现用户界面的各种组件。这些控件包括但不限于按钮、文本框、列表视图等。熟悉并掌握这些控件对于...

    android开发之控件一

    本篇将详细介绍几个基础且常用的Android控件,包括Button、TextView、EditText、CheckBox、RadioButton、ImageButton、ToggleButton以及ImageView。 **Button**(按钮)是用户触发操作的常见元素。创建Button的步骤...

    Android常用基本控件

    ### Android常用基本控件 #### 一、文本控件(TextView和EditText) **1.1 TextView控件** - **简介**:`TextView`是Android中最基础的文本显示控件,用于展示不可编辑的文字内容。 - **特点**: - 继承自`View`...

    Android 所有控件的使用

    5. **复选框(CheckBox)**和**单选按钮(RadioButton)**:这两种控件常用于提供多选或单选选项,可以组成一个RadioGroup或CheckBoxGroup来管理它们的行为。 6. **切换开关(Switch/ToggleButton)**:Switch提供了开/关...

    Android UI控件集合

    Android UI控件集合,包括Button,TextView,EditView,CheckBox,RadioGroup,Spinner,AutoCompleteTextView,DatePicker,TimePicker,ProgressBar,SeekBar,RatingBar,ImageView,ImageButton,ImageSwicrher,Gallery,...

    Android-UI基本控件

    图片按钮(ImageButton)的使用和Button控件类似,不同之处在于ImageButton通常用于需要展示图片的场景,用户可以点击图片执行相应的操作。 ImageView控件专门用于显示图片,开发者可以通过代码或XML布局文件设置要...

    Android 控件详细介绍.ppt

    android 开发常用到的控件 ,这里做了详细的解释. 文本控件 TextView EditText 按钮控件 Button ImageButton 状态开关按钮 ToggleButton 单选与复选按钮 CheckBox和RadioButton 图片控件 ImageView 时钟控件 ...

    android控件的使用

    `View`类的子类中,`TextView`、`ImageView`、`Button`、`CheckBox`等是最常见的控件,它们各自负责不同的功能,如文本显示、图片展示、按钮点击等。此外,还有一些高级控件,如`AutoCompleteTextView`、`SeekBar`、...

    Android 控件

    在Android应用开发中,控件(View)是构建用户界面的基本元素,它们提供了与用户交互的方式。本文将详细介绍几种常见的Android控件及其使用方法,特别适合Android初学者学习和参考。 1. **TextView**:TextView是...

    Android 中常见控件参考手册-中文版

    Android应用开发中,控件是构建用户界面的基本元素,决定了应用的外观和功能。本文档旨在详细介绍Android中一些常见控件的使用方法,帮助开发者更好地理解和运用它们。 1. **TextView文本框**: - TextView是用于...

    android基本的UI控件和布局文件知识要点

    - **android:layout_margin**:设定控件四周的边距,例如`android:layout_marginLeft="5dip"`表示控件左边与父容器的左侧边距为5dip。 2. **RelativeLayout**:相对布局,允许控件基于其他控件或其父容器进行定位...

    Android 中常见控件参考手册

    ### Android中常见控件参考手册知识点详述 #### TextView 文本框 - **类结构**:`TextView`是`View`的子类,用于显示文本信息。它支持丰富的文本格式和样式,包括字体大小、颜色、对齐方式等。 - **方法**:`...

    android控件中英对照

    标题与描述:“Android控件中英对照” 在深入解析标题与描述所指的“Android控件中英对照”之前,我们首先需要理解Android系统及其应用开发环境的基本概念。Android是Google开发的一款基于Linux内核的操作系统,...

    OPHONE &android常用控件

    法大同小异,这里仅以Button和EditText为例进行详细解释。在OPhone与Android平台上,这些基本...理解并熟练运用这些控件是开发OPhone和Android应用的基础,开发者可以根据需求组合这些组件,创建出丰富多样的用户界面。

    编写微信界面(UI界面设计-移动平台开发技术-gddrxy

    1) 文本类控件 :TextView 负责展示文本,非编辑 ;EditText 可编辑文本控件 。 2) 按钮类控件 :Button 按钮 ;ImageButton 图片按钮 ;RadioButton与RadioGroup 单选按钮 ;CheckBox 复选按钮 ; 3) 图片控件 :...

    android控件

    以上只是Android控件的一小部分,还有许多其他控件如ImageView(显示图片)、TextView(显示纯文本)、SwipeRefreshLayout(下拉刷新)等。理解并熟练使用这些控件,能够帮助开发者构建功能丰富且用户体验良好的...

    安卓中文API文档

    本文档涵盖了 Android 2.2 版本的 API,包括 TextView、EditText、AccessibilityService、Manifest、View、ImageView、ImageButton、QuickContactBadge、ZoomButton、CheckBox、RadioButton、Button、ToggleButton、...

Global site tag (gtag.js) - Google Analytics