赞
踩
属性 | 含义 |
---|---|
text | 文本内容 |
textSize | 文本字号,单位:sp |
textColor | 文本颜色,#ff0000 - 红色 |
layout_heigh | 高度,单位:dp (wrap_content, match_parent) |
layout_weight | 宽度,单位:dp (wrap_content, match_parent) |
属性 | 含义 |
---|---|
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 | 输入类型(普通文本、密码、邮件……) |
属性 | 含义 |
---|---|
text | 文本内容 |
textSize | 文本字号,单位:sp |
textColor | 文本颜色,#ff0000 - 红色 |
background | 背景颜色或背景图片 |
layout_height | 高度,单位:dp (wrap_content, match_parent) |
layout_weight | 宽度,单位:dp (wrap_content, match_parent) |
序号 | 任务 |
---|---|
1 | 在界面类里声明按钮控件变量 |
2 | 通过findViewById()方法得到按钮实例 |
3 | 利用setOnClickListener()方法给按钮注册单击事件监听器 |
4 | 实现单击事件监听器接口,采用匿名实现方式 |
5 | 在接口的抽象方法里编写事件处理代码 |
<?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:background="@drawable/background"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp"
tools:context=".LoginActivity">
<TextView
android:id="@+id/tv_user_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user_login"
android:textColor="#0000ff"
android:textSize="25sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/username"
android:textColor="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/input_username"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/password"
android:textColor="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/input_password"
android:inputType="textPassword"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="@string/login"
android:textSize="20sp" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="@string/cancel"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
属性 | 作用 |
---|---|
gravit | 用于设置容器的子控件的对齐方式,或控件的内容的对齐方式 |
orientation | 线性布局的方向(horizontal——横向、vertical——纵向) |
padding | 内边距,用于设置子控件与父容器边框的距离,或控件的内容与控件边框的距离;(paddingLeft、paddingRight、paddingTop、paddingBottom) |
layout_margin | 外边距,用于设置控件之间的距离;(layout_marginLeft,layout_marginRight、layout_marginTop、layout_marginBottom) |
<?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:background="@drawable/background"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#0000ff"/>
</LinearLayout>
<resources>
<string name="app_name">用户登录</string>
<string name="user_login">用户登录</string>
<string name="username">用户:</string>
<string name="input_username">请输入用户名</string>
<string name="password">密码:</string>
<string name="input_password">请输入密码</string>
<string name="login">登录</string>
<string name="cancel">取消</string>
</resources>
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tvMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 利用布局资源文件设置用户界面
setContentView(R.layout.activity_main);
// 通过资源标识符获取控件实例
tvMessage = findViewById(R.id.tv_message);
// 获取意图
Intent intent = getIntent();
// 判断意图是否为空
if (intent != null) {
// 获取意图携带的数据
String username = intent.getStringExtra("username");
String password = intent.getStringExtra("password");
// 拼接用户信息
String message = "登录成功!\n用户:" + username + "\n密码:" + password;
// 设置标签属性,显示用户信息
tvMessage.setText(message);
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。