目录
作业:注册界面及跳转
- 首先创建两个Activity模板,这里我创建的是
RegisterAc以及 ZCCG ,一个应用于注册界面,另一个应用于注册成功后所跳转的界面。
- 先把注册的基础界面弄出来,所以先编写创建Activity时生成的layout里所对应的文件。
下面展示一些内联代码片
。
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@mipmap/backg1"
android:padding="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/Re_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Re_name"
android:textColor="#000000"
android:textSize="20sp"/>
<EditText
android:id="@+id/edt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/Re_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Re_sex"
android:textColor="#000000"
android:textSize="20sp"/>
<EditText
android:id="@+id/edt_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/Re_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Re_age"
android:textColor="#000000"
android:textSize="20sp"/>
<EditText
android:id="@+id/edt_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/Re_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Re_phone"
android:textColor="#000000"
android:textSize="20sp"/>
<EditText
android:id="@+id/edt_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/Re_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Re_email"
android:textColor="#000000"
android:textSize="20sp"/>
<EditText
android:id="@+id/edt_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/Re_index"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Re_index"
android:textColor="#000000"
android:textSize="20sp"/>
<EditText
android:id="@+id/edt_index"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:orientation="horizontal">
<TextView
android:id="@+id/Re_remark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Re_remark"
android:textColor="#000000"
android:textSize="20sp"/>
<EditText
android:id="@+id/edt_remark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/btn_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="@string/register"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="@string/cancel"
android:textSize="20sp"/>
</LinearLayout>
</LinearLayout>
然后在Values 的strings里面添加会用到的数据
3.把应用优先显示设置为RegisterAc
打开运行后
4.再设置RegisterAc里的代码
下面展示一些 内联代码片
。
值得注意的是findViewById 一定要把相应的值都获取到声明的私有变量那,比如如果最后忘了获取按钮的id给声明的相应变量,可能会导致无法打开应用, 会出现 KEPPS STOPPING等提示
package net.zxj.demo0303;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
public class RegisterAc extends AppCompatActivity {
private EditText edtRename;
private EditText edtResex;
private EditText edtReage;
private EditText edtRephone;
private EditText edtReemail;
private EditText edtReindex;
private EditText edtReremark;
private Button btnRegister;
private Button btnCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//在活动栏显示图标
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setLogo(R.mipmap.ic_launcher);
edtRename =findViewById(R.id.edt_name);
edtResex = findViewById(R.id.edt_sex);
edtReage = findViewById(R.id.edt_age);
edtRephone = findViewById(R.id.edt_phone);
edtReemail = findViewById(R.id.edt_email);
edtReindex = findViewById(R.id.edt_index);
edtReremark = findViewById(R.id.edt_remark);
btnRegister = findViewById(R.id.btn_register);
btnCancel = findViewById(R.id.btn_cancel);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String strRename = edtRename.getText().toString().trim();
String strResex = edtResex.getText().toString().trim();
String strReage = edtReage.getText().toString().trim();
String strRephone = edtRephone.getText().toString().trim();
String strReemail = edtReemail.getText().toString().trim();
String strReremark = edtReremark.getText().toString().trim();
String strReindex = edtReindex.getText().toString().trim();
Intent intentRe = new Intent(RegisterAc.this,ZCCG.class);
//创建数据包,封装数据
Bundle re =new Bundle();
re.putString("Rename",strRename);
re.putString("Reage",strReage);
re.putString("Rephone",strRephone);
re.putString("Reemail",strReemail);
re.putString("Reremark",strReremark);
re.putString("Resex",strResex);
re.putString("Reindex",strReindex);
//通过意图启动目标组件
intentRe.putExtras(re);
startActivity(intentRe);
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
5.主界面设置好后,就到了设置跳转界面的ZCCG了
敲好后,再去设置跳转界面的显示内容
有想修饰的就可以在这里面添加。
输入信息后,点击注册就会跳转
这样就实现了简单的注册界面以及点击注册按钮进行跳转
总结
学习了启动模式,以及事件处理、窗口跳转与数据传递,现在能够完成简单的注册界面,对安卓应用开发有了初步的了解。
总的来说目前还是很简单的,就这个注册界面而言,我总结的完成步骤的框架:创建两个Activity模板,一主,一跳转 > 在layout里设置主界面的显示内容 > 在主界面的Java代码里设置按钮的监听函数 >设置数据包封装数据,启动意图 > 在跳转界面的Java代码里编写获取意图等代码