[Android开发基础2] 七大常用界面控件(附综合案例)

文章目录

 一、文本TextView

二、按钮Button

三、编辑输入框EditText

四、图片ImageView

五、单选按钮RadioButton

六、复选框CheckBox

七、系统消息框Toast

综合案例:账号注册界面

 


 

 一、文本TextView

TextView控件用于显示文本信息。

 演示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="追风赶月莫停留,平芜尽处是春山。"
        android:layout_gravity="center"
        android:textSize="25sp"
        android:textColor="@color/red"
        android:textStyle="italic">
    </TextView>
</RelativeLayout>

 

 属性:

 


二、按钮Button

        Button控件表示按钮,它继承自TextView控件,既可以显示文本,又可以显示图片,同时也允许用户通过点击来执行操作,当Button控件被点击时,被按下与弹起的背景会有一个动态的切换效果,这个效果就是点击效果 。

演示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击试试">
    </Button>
</RelativeLayout>

 点击事件实现方式:

方法1

 

方法2
方法3

 


三、编辑输入框EditText

EditText表示编辑框,它是TextView的子类,用户可在此控件中输入信息。

演示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   <EditText
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="请输入你的姓名"
       android:textColor="#767676"
       android:maxLines="2"
       android:textSize="18sp"
       ></EditText>
</RelativeLayout>

属性:


 

四、图片ImageView

         ImageView表示图片,它继承自View,可以加载各种图片资源,图片资源存放在资源文件夹res-->drawable文件夹下。

演示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   <ImageView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:background="@drawable/tom"
       ></ImageView>
</RelativeLayout>

属性:


 

五、单选按钮RadioButton

        RadioButton为单选按钮,android:checked 属性指定是否选中的状态。RadioGroup是单选组合框,可容纳多个RadioButton,并把它们组合在一起,实现单选状态。

演示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   <RadioGroup
       android:layout_width="match_parent"
       android:layout_height="wrap_content">
       <RadioButton
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="男"
           android:textSize="25sp"
           ></RadioButton>
       <RadioButton
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="女"
           android:textSize="25sp"
           ></RadioButton>
   </RadioGroup>
</RelativeLayout>

rg是按钮组对象,调用监听器的过程:

rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId==R.id.RB1){
                    msg1="男";
                }else if(checkedId==R.id.RB2){
                    msg1="女";
                }
            }
        });

六、复选框CheckBox

        CheckBox表示复选框,它是Button的子类,用于实现多选功能,通过android:checked属性指定CheckBox控件是否选中的状态。

演示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
   <CheckBox
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="打篮球"
       android:textSize="20sp">
   </CheckBox>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="听音乐"
        android:textSize="20sp">
    </CheckBox>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读好书"
        android:textSize="20sp">
    </CheckBox>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="吃美食"
        android:textSize="20sp">
    </CheckBox>
</LinearLayout>

复选框选中事件处理:

CompoundButton.OnCheckedChangeListener on=new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(buttonView.getId()==R.id.ck1){
                    msg2=isChecked ? "打篮球":null;
                }else  if(buttonView.getId()==R.id.ck2){
                    msg3=isChecked ? "听音乐":null;
                }else  if(buttonView.getId()==R.id.ck3){
                    msg4=isChecked ? "读好书":null;
                }else  if(buttonView.getId()==R.id.ck4){
                    msg5=isChecked ? "吃美食":null;
                }
            }
        };
ck1.setOnCheckedChangeListener(on);
ck2.setOnCheckedChangeListener(on);
ck3.setOnCheckedChangeListener(on);
ck4.setOnCheckedChangeListener(on);

七、系统消息框Toast

        Toast是Android系统提供的轻量级信息提醒机制,用于向用户提示即时消息,它显示在应用程序界面的最上层,显示一段时间后自动消失不会打断当前操作,也不获得焦点

用法:

Toast.makeText(this, "欢迎使用", Toast.LENGTH_SHORT).show();

 


综合案例:账号注册界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="账号注册"
        android:textSize="25sp"
        android:padding="10dp"
        android:textColor="@color/white"
        android:background="#2666ab"
        android:layout_gravity="center">
    </TextView>
    <ImageView
        android:layout_gravity="center"
        android:src="@drawable/qq"
        android:layout_height="100dp"
        android:layout_width="100dp">
    </ImageView>
    <LinearLayout
        android:layout_gravity="center"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:text="用户名:"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>
        <EditText
            android:hint="请输入你的用户名"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </EditText>
    </LinearLayout>
    <LinearLayout
        android:layout_gravity="center"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:text="密 码:"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>
        <EditText
            android:inputType="numberPassword"
            android:hint="请输入你的密码"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </EditText>
    </LinearLayout>
    <LinearLayout
        android:layout_gravity="center"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:text="性 别:"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>
        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="男">
            </RadioButton>
            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="女">
            </RadioButton>
        </RadioGroup>
    </LinearLayout>
    <LinearLayout
        android:layout_gravity="center"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:text="爱 好:"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>
        <CheckBox
            android:text="篮球"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </CheckBox>
        <CheckBox
            android:text="音乐"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </CheckBox>
        <CheckBox
            android:text="阅读"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </CheckBox>
    </LinearLayout>
    <LinearLayout
        android:layout_gravity="center"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:textSize="20sp"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="15dp"
            android:backgroundTint="#2666ab"
            android:text="注册">
        </Button>
        <Button
            android:textSize="20sp"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:backgroundTint="#e9003b"
            android:text="取消">
        </Button>
    </LinearLayout>
</LinearLayout>

END.

  • 5
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
### 回答1: 《Android移动开发基础案例教程第二版》是一本非常实用的Android开发指南,提供了丰富的基础案例和教程,帮助读者提高Android开发技能。 该书配合源码使用,学习者可根据书中教程,从源码中学习如何使用Android SDK进行开发。源码中包含了丰富的示例代码,如UI控件基础、布局管理器、事件处理、Fragment碎片、Intent启动、ListView列表显示、GridView网格显示、RecyclerView列表及网格等常用控件使用方法,通过运行源码,读者可以更加清楚地理解这些知识点的实际应用场景。 同时,源码中也提供了完整的工程结构,读者可以从项目中学习如何搭建Android应用,如何进行资源文件的管理和使用等,为日后开发奠定了扎实的基础。 总之,《Android移动开发基础案例教程第二版》配合源码使用,可以帮助读者迅速掌握开发Android应用的基础技能,在实践中积累经验,为日后的Android开发之路打下坚实的基础。 ### 回答2: 《Android移动开发基础案例教程(第二版)》是一本针对初学者编写的Android开发指南。本书的源码包含了许多实用案例,可以帮助读者更好地学习和掌握Android开发基础知识。 本书的源码总共包含了16个案例,其中包括了Activity、Intent、UI设计、布局、数据存储、网络通信等多个方面的内容。每个案例都循序渐进地展示了整个开发过程,从界面设计到逻辑代码的实现,再到测试和发布,全方位地让读者理解每一步。 此外,本书的源码也具有高精度、高效率、易扩展的特点,并且代码注释非常详细,使得读者可以快速了解每个函数和变量的作用,加深对代码的理解。 总体来说,《Android移动开发基础案例教程(第二版)》是一本优秀的Android开发入门指南,通过学习书中的源码,读者不仅可以熟悉Android开发基础知识,还可以掌握开发过程中的一些规范和最佳实践,从而更加高效地进行开发。 ### 回答3: 《Android移动开发基础案例教程 第二版》是一本介绍Android开发基础知识的教材,它通过不同的案例来讲解Android开发常用的技术和工具。本书的第二版比第一版更新了更多的实例代码,同时更新了对Android Studio工具的介绍。 本书的代码清晰易懂,是初学者入门Android开发的好教材。该书源码可以在GitHub上下载,包含了本书中所有案例的完整代码和资源文件。学习者可以通过运行源码来快速了解案例的实现原理,并且可以自己进行修改和改进。 本书的案例涉及的知识点包括了Android界面设计、多媒体开发、网络编程等多个方面。通过学习这些案例,读者可以逐步掌握Android开发的关键技术,提升自己的编程能力。 总之,通过学习《Android移动开发基础案例教程 第二版》的源码,读者可以快速入门Android开发,掌握关键的技术和工具,同时也可以通过源码的改进和扩展来深入学习Android开发

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aricl.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值