安卓开发 5.2初步了解编辑框的使用

一、导读

  • 在安卓应用中经常需要用户输入信息,比如用户名、密码、电话号码之类,一般采用编辑框来接收用户输入的任何文本信息。

二、编辑框常用属性

text文本内容
textSize文本字号,单位:sp
textColor文本颜色,#ff0000 - 红色
hint提示信息
singleLine单行(true or false)
layout_height高度,单位:dp (wrap_content, match_parent)
layout_weight宽度,单位:dp (wrap_content, match_parent)
inputType输入类型(普通文本、密码、邮件……)
maxLines最大行数
lines行数

三、乘法运算程序

1、准备工作

  1. 创建安卓应用:于Empty Activity模板创建安卓应用 - Arithmeticprogram

2、字符串资源在这里插入图片描述

3、布局资源文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        >

        <EditText
            android:id="@+id/tv_first"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:singleLine="true"
            android:layout_marginEnd="10dp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/multiply"
            android:layout_marginEnd="10dp"
            />

        <EditText
            android:id="@+id/tv_cat"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:singleLine="true"
            android:layout_marginEnd="10dp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/amount"
            android:layout_marginEnd="10dp"
            />

        <TextView
            android:id="@+id/tv_result"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:singleLine="true"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center">

        <Button
            android:id="@+id/bt_compute"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doCompute"
            android:text="@string/compute"
            android:layout_marginEnd="10dp"/>

        <Button
            android:id="@+id/bt_purge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doPurge"
            android:text="@string/purge"
            android:layout_marginEnd="10dp"/>

        <Button
            android:id="@+id/tv_exit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doCancel"
            android:text="@string/exit"
            android:layout_marginEnd="10dp"/>

    </LinearLayout>

</LinearLayout>

在这里插入图片描述

4、计算类的实现

(1)声明变量

在这里插入图片描述

(2)通过资源标识符获取控件实例

在这里插入图片描述

(3)编写计算,清空,退出单击按钮的事件

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

5、启动应用查看效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值