一、基本控件
1、标签控件(TextView)
常用属性:text、textSize、textColor……
2、编辑框控件(EditText)
常用属性:text、textSize、textColor、hint……
3、按钮控件(Button)
常用属性:text、background、layout_width、layout_height……
二、知识点
线性布局(LinearLayout)
标签(TextView)
编辑框(EditText)
按钮(Button)
安卓事件处理机制(Event Handling Mechanism)
吐司(Toast)
三、创建安卓应用
四、添加背景图片
最好添加两张背景图片(一张用于登录与注册,第二张用于实现跳转页面)
五、基于模板创建登录窗口
用于页面跳转实现的窗口
六、登录窗口布局资源文件
<?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:gravity="center"
android:orientation="vertical"
android:background="@drawable/tt"
tools:context=".Loginu">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="400dp"
android:layout_weight="2"
android:gravity="bottom"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_user_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user_login"
android:textColor="#9C27B0"
android:textSize="35dp">
</TextView>
</LinearLayout>>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="5"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="@+id/tvusername"
android:layout_width="90dp"
android:layout_height="60dp"
android:ems="5"
android:gravity="center"
android:text="@string/username"
android:textSize="20dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="60dp"
android:singleLine="true"
android:id="@+id/edt_username"
android:ems="10"
android:hint="@string/input_username"
android:inputType="textPersonName"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="5"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_password"
android:layout_width="90dp"
android:layout_height="60dp"
android:ems="10"
android:gravity="center"
android:text="@string/password"
android:textSize="20dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="60dp"
android:id="@+id/edt_password"
android:ems="10"
android:hint="@string/input_password"
android:singleLine="true"
android:inputType="textPassword"
/>
</LinearLayout>
<LinearLayout
android:layout_width="230dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/custom_border"
android:hint="登录"
android:textColor="#130908"
android:textSize="25dp"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:background="@drawable/custom_border"
android:hint="取消"
android:textColor="#130908"
android:textSize="25dp" />
</LinearLayout>
</LinearLayout>
七、布局资源文件
<?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/flow"
android:orientation="vertical"
android:padding="23dp"
tools:context=".User">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvusername"
android:layout_marginTop="10dp"
android:textColor="@color/colorAccent"
android:textSize="25dp"/>
</LinearLayout>
八、字符串资源文件strings.xml
<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>
</resources>
九、登录窗口Loginu
(一)、利用布局资源文件设置用户界面
(二)、声明控件变量
(三)、 通过资源标识符获取控件实例
(四)、给登录按钮注册监听器,实现监听器接口,编写事件处理方法
(五)、取消按钮事件处理
(六)、在活动栏上显示图标
源代码:
package net.lht.fuxi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Loginu extends AppCompatActivity {
private EditText edt_username;
private EditText edt_password;
private Button btnbutton1;
private Button btnbutton2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//利用布局资源文件设置用户界面
setContentView(R.layout.activity_loginu);
//通过资源标识获得控件实例
edt_username = findViewById(R.id.edt_username);
edt_password = findViewById(R.id.edt_password);
btnbutton1 = findViewById(R.id.button1);
btnbutton2 = findViewById(R.id.button2);
//给登录按钮注册监听器,实现监听器接口,编写事件处理方法
btnbutton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//保存用户输入的数据:用户名与密码
String strUsername = edt_username.getText().toString().trim();
String strPassword = edt_password.getText().toString().trim();
//判断用户名与密码是否正确(假定用户名与密码都是"admin")
if (strUsername.equals("admin") && strPassword.equals("admin")){
Toast.makeText( Loginu.this,
"恭喜.用户名与密码正确!",Toast.LENGTH_SHORT).show();
//创建显式意图(参数1:包上下文;参数2:目标组件)
Intent intent = new Intent(Loginu.this,User.class);
intent.putExtra("username",strUsername);
intent.putExtra("password",strPassword);
//按照意图启动目标组件
startActivity(intent);
} else {
Toast.makeText(Loginu.this,
"遗憾,用户名或密码错误!",Toast.LENGTH_SHORT).show();
}
}
});
//给取消按钮监听器,实现监听器接口,编写事件处理方法
btnbutton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//关闭登录窗口
finish();
}
});
//在活动栏上显示图标
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setLogo(R.mipmap.ic_launcher_my);
}
}
(七)、登录成功页面
package net.lhf.fuxi;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class User extends AppCompatActivity {
private TextView tvusername;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
tvusername = (TextView) findViewById(R.id.tvusername);
Intent intent = getIntent();
if (intent != null) {
// 获得意图携带的数据包
Bundle bundle = intent.getExtras();
String strUsername = bundle.getString("username");
//拼接用户信息
//String username = "登录成功!\n 用户:"+strUsername;
tvusername.setText("登录成功:"+strUsername);
}
}
}
十、基于模板创建注册窗口与实现页面
十一、注册窗口布局资源文件
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edtName"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/tvGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gender"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edtGender"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/tvAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/age"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edtAge"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/tvPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/phone"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edtPhone"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/tvEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/email"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edtEmail"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/tvHomePage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/home_page"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edtHomePage"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textUri"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/tvMemo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/memo"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edtMemo"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:lines="4" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/btnRegister"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:onClick="doRegister"
android:text="@string/register" />
<Button
android:id="@+id/btnCancel"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:onClick="doCancel"
android:text="@string/cancel" />
</LinearLayout>
十二、布局资源文件(注册页面实现) ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210112175039127.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0x5ZGlhbGhm,size_16,color_FFFFFF,t_70)
<resources>
<string name="button2">注册</string>
<string name="app_n">用户注册</string>
<string name="name">姓名:</string>
<string name="gender">性别:</string>
<string name="age">年龄:</string>
<string name="phone">电话:</string>
<string name="email">邮箱:</string>
<string name="home_page">主页:</string>
<string name="memo">备注:</string>
<string name="register">注册</string>
<string name="cancel">取消</string>
</resources>
十四、注册窗口
package net.lhf.fuxi;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class Registration extends AppCompatActivity {
private EditText edtName;
private EditText edtGender;
private EditText edtAge;
private EditText edtPhone;
private EditText edtEmail;
private EditText edtHomePage;
private EditText edtMemo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//利用布局资源文件设置用户界面
setContentView(R.layout.activity_registration);
// 通过资源标识符获得控件实例
edtName = findViewById(R.id.edtName);
edtGender = findViewById(R.id.edtGender);
edtAge = findViewById(R.id.edtAge);
edtPhone = findViewById(R.id.edtPhone);
edtEmail = findViewById(R.id.edtEmail);
edtHomePage = findViewById(R.id.edtHomePage);
edtMemo = findViewById(R.id.edtMemo);
}
/**
* 注册按钮单击事件处理方法
*
* @param view
*/
public void doRegister(View view) {
// 获取用户输入数据
String strName = edtName.getText().toString();
String strGender = edtGender.getText().toString();
String strAge = edtAge.getText().toString();
String strPhone = edtPhone.getText().toString();
String strEmail = edtEmail.getText().toString();
String strHomePage = edtHomePage.getText().toString();
String strMemo = edtMemo.getText().toString();
// 将各项输入数据打包
Bundle data = new Bundle();
data.putString("name", strName);
data.putString("gender", strGender);
data.putString("age", strAge);
data.putString("phone", strPhone);
data.putString("email", strEmail);
data.putString("home_page", strHomePage);
data.putString("memo", strMemo);
// 创建意图,指定起始组件与目标组件
Intent intent = new Intent(Registration.this, information.class);
// 利用意图携带数据包
intent.putExtras(data);
// 按意图启动目标窗口
startActivity(intent);
}
/**
* 取消按钮单击事件处理方法
*
* @param view
*/
public void doCancel(View view) {
finish();
}
}
(一)、注册实现页面
package net.tp.fuxi;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class information extends AppCompatActivity {
private TextView tvName;
private TextView tvGender;
private TextView tvAge;
private TextView tvPhone;
private TextView tvEmail;
private TextView tvHomePage;
private TextView tvMemo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 利用布局文件设置用户界面
setContentView(R.layout.activity_information);
// 通过资源标识获得控件示例
tvName = (TextView) findViewById(R.id.tvName);
tvGender = (TextView) findViewById(R.id.tvGender);
tvAge = (TextView) findViewById(R.id.tvAge);
tvPhone = (TextView) findViewById(R.id.tvPhone);
tvEmail = (TextView) findViewById(R.id.tvEmail);
tvHomePage = (TextView) findViewById(R.id.tvHomePage);
tvMemo = (TextView) findViewById(R.id.tvMemo);
// 获得意图
Intent intent = getIntent();
if (intent != null) {
// 获得意图携带的数据包
Bundle bundle = intent.getExtras();
// 从数据包里按键取值
String strName = bundle.getString("name");
String strGender = bundle.getString("gender");
String strAge = bundle.getString("age");
String strPhone = bundle.getString("phone");
String strEmail = bundle.getString("email");
String strHomePage = bundle.getString("home_page");
String strMemo = bundle.getString("memo");
// 设置各个标签的内容
tvName.setText("姓名:" + strName);
tvGender.setText("性别:" + strGender);
tvAge.setText("年龄:" + strAge);
tvPhone.setText("电话:" + strPhone);
tvEmail.setText("邮箱:" + strEmail);
tvHomePage.setText("主页:" + strHomePage);
tvMemo.setText("备注:" + strMemo);
}
}
}
十五、运行效果