实验2 Activity及常用布局和控件的使用
实验2 Activity及常用布局和控件的使用
一.实验目的
掌握Android常用布局和控件的使用。Activity组件使用和Intent机制,加强对Activity生命周期的理解
二.实验设备及器件
Android studio
三.实验内容
1.创建一个Android应用,创建一个Android应用,创建MainActivity和LoginActivity,抽取基类BaseActivity
2.在MainActivity中onCreate,onStart,onResume,onPause,onStop,onDestory,onRestart生命周期方法打印log,运行App,观察不同操作下Activity的生命周期调用时机和顺序并在实验报告中描述出来。
3.在MainActivity中打开LoginActivity,并观察参数user_name,值“001”,将其显示在LoginActivity页面中的用户名输入框中。
4.LoginActivity继承BaseActivity,根据所给UI实现以下LoginActivity页面,点击登陆按钮检测账号密码输入是否为空,如果为空则弹出Toast提示“账号不能为空”“密码不能为空",如果不为空,弹出对话框显示账号密码。
四.实验步骤
1.新建工程编写代码
LoginActivity.java
public class LoginActivity extends BaseActivity implements View.OnClickListener {
private static final String TAG = "LoginActivity";
private EditText et_username;
private EditText et_pwd;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// toastShort("tanchutoast");
String user_name = getIntent().getStringExtra("user_name");
Log.d(TAG, "user_name = " + user_name);
initView();
}
private void initView() {
findViewById(R.id.iv_back).setOnClickListener(this);
findViewById(R.id.bt_login).setOnClickListener(this);
et_username = findViewById(R.id.et_username);
et_pwd = findViewById(R.id.et_pwd);
String user_name = getIntent().getStringExtra("user_name");
Log.d(TAG, "user_name = " + user_name);
et_username.setText(user_name);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_back:
finish();
break;
case R.id.bt_login:
String user_name = et_username.getText().toString();
String pwd = et_pwd.getText().toString();
if (TextUtils.isEmpty(user_name)) {
toastShort("用户名不能为空");
return;
}
if (TextUtils.isEmpty(pwd)) {
toastShort("密码不能为空");
return;
}
new AlertDialog.Builder(this)
.setTitle("提示")
.setMessage("用户名:" + user_name + "密码:" + pwd)
.show();
break;
}
}
}
activity_login.xml
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="登录"
android:textColor="@color/title_text"
android:textSize="16sp" />
<ImageView
android:id="@+id/iv_back"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="16dp"
android:src="@mipmap/ll" />
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f1f1f1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="12dp"
android:paddingRight="12dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:"
android:textColor="#252525"
android:textSize="15sp" />
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:hint="用户名/邮箱/手机号"
android:textColor="#252525"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="1dp"
android:background="@android:color/white"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="12dp"
android:paddingRight="12dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textColor="#252525"
android:textSize="15sp" />
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:hint="请输入密码"
android:inputType="textPassword"
android:textColor="#252525"
android:textSize="15sp" />
</LinearLayout>
<Button
android:id="@+id/bt_login"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:background="@drawable/bt_bg"
android:padding="12dp"
android:text="登录"
android:textColor="#ffffff"
android:textSize="15sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="快速注册"
android:textColor="#252525"
android:textSize="15sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="找回密码"
android:textColor="#252525"
android:textSize="15sp" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
MainActivity.java
package com.example.sky.one;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.bt_button).setOnClickListener(this);
Log.d(TAG,"onCreate");
// toastShort("MainActivity");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG,"onRestart");
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG,"onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG,"onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG,"onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG,"onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG,"onDestroy");
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_button:
Intent intent =new Intent(MainActivity.this,LoginActivity.class);
intent.putExtra("user_name","001");
startActivity(intent);
break;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/bt_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
/>
</LinearLayout>
2.模拟器运行结果展示
五.总结
例1:
if (TextUtils.isEmpty(user_name)) {//
toastShort("用户名不能为空");
return;
}
1.TextUtils.isEmpty(charSequence str)这个方法是系统为我们提供的一个非常方便的判断一个CharSequence类型的参数是否为空,这个方法返回的一个boolean,当括号里面的参数为(null)或者("")时,返回true.
2.TextUtils.equals(CharSequence str1,CharSequence str2),这个是比较括号里面两个CharSequence类型的参数是否相等。
例2:
new AlertDialog.Builder(this)
.setTitle("提示")
.setMessage("用户名:" + user_name + "密码:" + pwd)
.show();