Android的基本控件及用法和其他一些知识

1. 文本类控件TextView

TextView是 Android 程序开发中最常用的控件之一,主要功能是向用户展示文本的内容,它是不可编辑的 ,只能通过初始化设置或在程序中修改。

以下介绍一些常见的属性,更多属性可以参考TextView属性大全

<TextView

//控件id
android:id = "@+id/xxx"  @+id/xxx表示新增控件命名为xxx

//宽度与高度
android:layout_width="wrap_content"  //wrap_content或者match_parent
android:layout_height="wrap_content"  //wrap_content或者match_parent

//文本文字 
android:text="@string/hello_world" //两种方式,直接具体文本或者引用values下面的string.xml里面的元素

//字体大小
android:textSize="24sp"  //以sp为单位

//字体颜色
android:textColor="#0000FF"  //RGB颜色

//字体格式
android:textStyle="normal"  //normal,bold,italic分别为正常,加粗以及斜体,默认为normal

//文本显示位置
android:gravity="center"  //来指定文字的对齐方式,可选值有 top、bottom、left、right、center 等

//是否只在一行内显示全部内容
android:singleLine="true"  //true或者false,默认为false

1. 文本类控件TextView

TextView是 Android 程序开发中最常用的控件之一,主要功能是向用户展示文本的内容,它是不可编辑的 ,只能通过初始化设置或在程序中修改。

以下介绍一些常见的属性,更多属性可以参考TextView属性大全

<TextView

//控件id
android:id = "@+id/xxx"  @+id/xxx表示新增控件命名为xxx

//宽度与高度
android:layout_width="wrap_content"  //wrap_content或者match_parent
android:layout_height="wrap_content"  //wrap_content或者match_parent

//文本文字 
android:text="@string/hello_world" //两种方式,直接具体文本或者引用values下面的string.xml里面的元素

//字体大小
android:textSize="24sp"  //以sp为单位

//字体颜色
android:textColor="#0000FF"  //RGB颜色

//字体格式
android:textStyle="normal"  //normal,bold,italic分别为正常,加粗以及斜体,默认为normal

//文本显示位置
android:gravity="center"  //来指定文字的对齐方式,可选值有 top、bottom、left、right、center 等

//是否只在一行内显示全部内容

android:singleLine="true" //true或者false,默认为false

3.按钮类控件Button

Button控件也是使用过程中用的最多的控件之一,所以需要好好掌握。用户可以通过单击 Button 来触发一系列事件,然后为 Button 注册监听器,来实现 Button 的监听事件。

首先来看下Button的配置属性,其实和TextView差不多设置更简单点,主要是显示到Button上面的文字提示:

<Button

//控件id
android:id = "@+id/xxx"  @+id/xxx表示新增控件命名为xxx

//宽度与高度
android:layout_width="wrap_content"  //wrap_content或者match_parent
android:layout_height="wrap_content"  //wrap_content或者match_parent

//按钮上显示的文字 
android:text="theButton" //两种方式,直接具体文本或者引用values下面的string.xml里面的元素@string/button

//按钮字体大小
android:textSize="24sp"  //以sp为单位

//字体颜色
android:textColor="#0000FF"  //RGB颜色
public class MainActivity extends Activity {
    private EditText edittext;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edittext=(EditText) findViewById(R.id.edit_text);
        button = (Button) findViewById(R.id.button);
        //为button按钮注册监听器,并通过匿名内部类实现
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            //点击Button会改变edittext的文字为"点击了Button"
            edittext.setText("点击了Button");
            }
        }); 
    }
}
   
   

    5.按钮类控件RadioButton与RadioGroup

    RadioButton(单选按钮)在 Android 平台上也比较常用,比如一些选择项会用到单选按钮。它是一种单个圆形单选框双状态的按钮,可以选择或不选择。在 RadioButton 没有 被选中时,用户通过单击来选中它。但是,在选中后,无法通过单击取消选中。

    RadioGroup 是单选组合框,用于 将 RadioButton 框起来。在多个 RadioButton被 RadioGroup 包含的情况下,同一时刻只可以选择一个 RadioButton,并用 setOnCheckedChangeListener 来对 RadioGroup 进行监听。

    下面介绍RadioGroup的常用的属性,因为其中包含有RadioButton:

     <RadioGroup
            android:id="@+id/radio_group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            //设置RadioButton的排列方式,分为水平排列horizontal与垂直排列vertical
            android:orientation="horizontal" >
               <RadioButton 
                   android:id="@+id/rd1"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   //设置单选后紧跟的文本提示文字
                   android:text="北京"
                   //设置文字的大小
                   android:textSize="30sp"
                   //设置文字的颜色
                   android:textColor="#0000FF"
                   //字体格式
                   android:textStyle="normal"  //normal,bold,italic分别为正常,加粗以及斜体,默认为normal
                    />
               <RadioButton 
                   android:id="@+id/rd2"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:textSize="30sp"
                   android:text="上海" />
          </RadioGroup>
       
       
    • 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

    下面给出在Activity中用 setOnCheckedChangeListener 来对 RadioGroup 进行监听的代码, 注意RadioGroup中的RadioButton也都是需要声明和通过控件的id来得到代表控件的对象。

    public class MainActivity extends Activity{
        对控件对象进行声明 
        private TextView textView;
        private RadioGroup radiogroup; 
        private RadioButton radiobutton1; 
        private RadioButton radiobutton2;  
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //通过控件的ID来得到代表控件的对象 
            textView = (TextView) findViewById(R.id.text_view);
            radiogroup = (RadioGroup) findViewById(R.id.radio_group);
            radiobutton1 = (RadioButton) findViewById(R.id.rd1);
            radiobutton2 = (RadioButton) findViewById(R.id.rd2);
    
            //调用setOnCheckedChangeListener来对RadioGroup进行监听的代码
            radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if(checkedId == radiobutton1.getId()){
                        textView.setText("北京");
                    }else if(checkedId == radiobutton2.getId()){
                        textView.setText("上海");
                    }
                }
            });
        }   
    }
       
       
    • 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

    6.按钮类控件CheckBox

    CheckBox(复选按钮),顾名思义是一种可以进行多选的按钮,默认以矩形表示。与 RadioButton 相同,它也有选中或者不选中双状态。我们可以先在布局文件中定义多选按钮, 然后对每一个多选按钮进行事件监听 setOnCheckedChangeListener,通过 isChecked 来判断 选项是否被选中,做出相应的事件响应。

    下面给出CheckBox在布局文件中的常用的属性以及用法:

    <CheckBox 
        android:id="@+id/cb1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        //设置复选按钮后紧跟的文本提示文字
        android:text="北京"
        //设置文字的大小
        android:textSize="30sp"
        //设置文字的颜色
        android:textColor="#0000FF"
        //字体格式
        android:textStyle="normal"  //normal,bold,italic分别为正常,加粗以及斜体,默认为normal/>
    <CheckBox 
        android:id="@+id/cb2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="上海"
        android:textSize="30sp"
        android:textColor="#0000FF"/>
       
       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在Activity中调用的代码如下:

    public class MainActivity extends Activity{
        对控件对象进行声明 
        private TextView textView;
        private CheckBox checkbox1;
        private CheckBox checkbox2; 
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //通过控件的ID来得到代表控件的对象 
            textView = (TextView) findViewById(R.id.text_view);
            checkbox1 = (CheckBox) findViewById(R.id.cb1);
            checkbox2 = (CheckBox) findViewById(R.id.cb2);
    
            //为第一个 CheckBox 注册监听
            checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    //如果第一个 CheckBox 被选中
                    if(isChecked == true){
                        textView.setText("CheckBox选中北京");
                    }
                }
            });
    
            //为第二个 CheckBox 注册监听
            checkbox2.setOnCheckedChangeListener(new OnCheckedChangeListener(){
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    //如果第二个 CheckBox 被选中
                    if(isChecked == true){
                        textView.setText("CheckBox选中上海");
                    }
                }
            });
        }   
    }
       
       
    • 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
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    7.图片控件ImageView

    ImageView 是一个图片控件,负责显示图片,图片的来源可以是系统提供的资源文件,也可以是 Drawable 对象,相对来说,图片空间还是比较好掌握的,因为前面有讲过ImageButton, 很多属性都是相同的。 
    下面直接给出在布局中的属性:

    <ImageView
    //控件id
    android:id = "@+id/xxx"  @+id/xxx表示新增控件命名为xxx
    
    //宽度与高度
    android:layout_width="wrap_content"   //wrap_content或者match_parent
    android:layout_height="wrap_content"  //wrap_content或者match_parent
    
    //此外,可以具体设置高度和宽度显示的像素,不过这样设置如果图片尺寸大于设置的显示的尺寸,则图片是显示不全的,这是可以配合android:scaleType属性。
    android:layout_width="200dp"
    android:layout_height="200dp" 
    
    //把原图按照指定的大小在View中显示,拉伸显示图片,不保持原比例,填满ImageButton.
    android:scaleType="fitXY"
    
    //其他的关于android:scaleType的参数解释,也可以参考下面的直观图
    //android:scaleType="center"  在视图中心显示图片,并且不缩放图片
    //android:scaleType="centercrop"  按比例缩放图片,使得图片长 (宽)的大于等于视图的相应维度
    //android:scaleType="centerinside"  按比例缩放图片,使得图片长 (宽)的小于等于视图的相应维度
    //android:scaleType="fitcenter" 按比例缩放图片到视图的最小边,居中显示
    //android:scaleType="fitend" 按比例缩放图片到视图的最小边,显示在视图的下部分位置
    //android:scaleType="fitstart" 把图片按比例扩大/缩小到视图的最小边,显示在视图的上部分位置
    //android:scaleType="matrix" 用矩阵来绘制
    
    //图片来源,需要将图片复制放到res/drawable文件夹里面,引用的时候不需要写图片的后缀
    android:src ="@drawable/beautiful">  
    
    


    ViewSwitcherandroid.widget.ViewSwitcher是ViewAnimator的子类,用于在两个View之间切换,但每次只能显示一个View。

    若View的数量超过两个,会抛出异常:java.lang.IllegalStateException,打印 "Can't add more than 2 views to a ViewSwitcher" 。你可以使用ViewSwitcher的factory创建View或添加自己创建的View。
    1. ?xml version="1.0" encoding="utf-8"?>  
    2.   
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    4.               xmlns:tools="http://schemas.android.com/tools"  
    5.               android:layout_width="match_parent"  
    6.               android:layout_height="match_parent"  
    7.               android:orientation="vertical"  
    8.               tools:context=".MainActivity" >  
    9.   
    10.   
    11.     <LinearLayout  
    12.             android:layout_width="match_parent"  
    13.             android:layout_height="wrap_content"  
    14.             android:orientation="horizontal" >  
    15.   
    16.         <Button  
    17.                 android:id="@+id/prev"  
    18.                 android:layout_width="0dp"  
    19.                 android:layout_height="wrap_content"  
    20.                 android:layout_weight="1"  
    21.                 android:text="previous" />  
    22.   
    23.         <Button  
    24.                 android:id="@+id/next"  
    25.                 android:layout_width="0dp"  
    26.                 android:layout_height="wrap_content"  
    27.                 android:layout_weight="1"  
    28.                 android:text="next" />  
    29.     </LinearLayout>  
    30.   
    31.     <ViewSwitcher  
    32.             android:id="@+id/viewswitcher"  
    33.             android:layout_width="match_parent"  
    34.             android:layout_height="wrap_content" >  
    35.   
    36.         <ImageView  
    37.                 android:layout_width="wrap_content"  
    38.                 android:layout_height="wrap_content"  
    39.                 android:src="@drawable/ic_launcher" />  
    40.   
    41.         <LinearLayout  
    42.                 android:layout_width="match_parent"  
    43.                 android:layout_height="wrap_content"  
    44.                 android:gravity="center"  
    45.                 android:orientation="vertical" >  
    46.   
    47.             <Button  
    48.                     android:layout_width="wrap_content"  
    49.                     android:layout_height="wrap_content"  
    50.                     android:text="- Button 2 -" />  
    51.   
    52.             <TextView  
    53.                     android:layout_width="wrap_content"  
    54.                     android:layout_height="wrap_content"  
    55.                     android:text="LinearLayout 2" />  
    56.         </LinearLayout>  
    57.     </ViewSwitcher>  
    58. </LinearLayout>  





    1. 传值,跳页面

    package com.exam.exam;


    import android.content.Intent;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.TextView;


    /**
     * Created by Administrator on 2017/12/22.
     */


    public class SecondActivity extends AppCompatActivity {
        private int sum;
        private RadioGroup mgroup_02;
        private TextView next_02,prev_02;
        private RadioButton rb_1,rb_2,rb_3,rb_4;
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.exam02);
            mgroup_02= (RadioGroup) findViewById(R.id.mgroup_02);
            next_02= (TextView) findViewById(R.id.next_02);
            rb_1= (RadioButton) findViewById(R.id.rb_1);
            rb_2= (RadioButton) findViewById(R.id.rb_2);
            rb_3= (RadioButton) findViewById(R.id.rb_3);
            rb_4= (RadioButton) findViewById(R.id.rb_4);
            Intent intent=getIntent();
            sum=intent.getIntExtra("res",0);
            next_02.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int rb=mgroup_02.getChildCount();
                    for (int i=0;i<rb;i++){
                        RadioButton rbtn= (RadioButton) mgroup_02.getChildAt(i);
                        boolean rbcheck=rbtn.isChecked();
                        if (rbcheck)
                        {
                            if (rbtn.getId()==R.id.rb_3)  {
                                sum+=25;
                            }
                            else {
                                sum+=0;
                            }
                        }
                    }
                    Intent intent=new Intent(SecondActivity.this,ThirdActivity.class);
                    intent.putExtra("res",sum);
                    SecondActivity.this.startActivity(intent);

                }
            });
        }
    }

    Android中的include的用法

    在Android的layout样式定义中,可以使用xml文件方便的实现,有时候为了模块的复用,使用include标签可以达到此目的。例如:

    <include layout="@layout/otherlayout"></div> 
    



    • 0
      点赞
    • 3
      收藏
      觉得还不错? 一键收藏
    • 0
      评论

    “相关推荐”对你有帮助么?

    • 非常没帮助
    • 没帮助
    • 一般
    • 有帮助
    • 非常有帮助
    提交
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值