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

来源:互联网 发布:北京大学大数据研究院 编辑:程序博客网 时间:2021/01/22 01:14

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

介绍

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


1、TextView 的 Demo
textview.xml
<?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>
<?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>
 
3、ImageButton 的 Demo
imagebutton.xml
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("图片按钮被单击了"); 
                        } 
                }); 
        } 
}
 
 
_ImageView.java
<?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>
 
6、RadioButton 的 Demo
radiobutton.xml
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() + " 被选中");                                         
                        } 
                });    
        } 
}
 
 
_AnalogClock.java
<?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>
 
OK
0 0