目录
一、导读
- 在安卓应用中经常需要用户输入信息,比如用户名、密码、电话号码之类,一般采用编辑框来接收用户输入的任何文本信息。
二、编辑框常用属性
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、准备工作
- 创建安卓应用:于
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)编写计算,清空,退出单击按钮的事件