用户注册及显示信息界面代码
java类MainActivity——代码
package net.cgh.userlongin;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//设置姓名编辑框
private EditText etdusername;
//设置性别编辑框
private EditText edtsex;
// 设置年龄编辑框
private EditText edtage;
//设置电话编辑框
private EditText edtall;
//设置邮邮箱编辑框
private EditText edtemali;
//设置主页编辑框
private EditText homepage;
//设置备注编辑框
private EditText noto;
//设置注册按钮
private Button btn1;
// 注册取消按钮
private Button btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//利用布局资源文件设置用户界面
setContentView(R.layout.activity_main);
//通过资源索引获得界面空间实例
etdusername = (EditText) findViewById(R.id.edtusername);
edtsex = (EditText) findViewById(R.id.edtsex);
edtage = (EditText) findViewById(R.id.edtage);
edtall = (EditText) findViewById(R.id.edttell);
edtemali = (EditText) findViewById(R.id.edtemail);
homepage =(EditText) findViewById(R.id.homepage);
noto = (EditText) findViewById(R.id.note);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
// 给注册按钮设置监听器
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 保存用户输入的数据:用户名和密码
String strUsername = etdusername.getText().toString().trim();
String strSex = edtsex.getText().toString().trim();
String strAge = edtage.getText().toString().trim();
String strTell = edtall.getText().toString().trim();
String strEmali = edtemali.getText().toString().trim();
String strHomepage = homepage.getText().toString().trim();
String strNote = noto.getText().toString().trim();
if ((!strUsername.equals(null))&&(!strSex.equals(null))&&(!strAge.equals(null))&&(!strTell.equals(null))
&&(!strEmali.equals(null))&&(!strHomepage.equals(null))&&(!strNote.equals(null))) {
Toast.makeText(MainActivity.this, "恭喜,注册成功!", Toast.LENGTH_LONG).show();
// 实现跳转窗口,创建意图(参数1:上下文 :参数2:目标文件)
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
//创建数据包
Bundle data = new Bundle();
// 将数据放入到数据包
data.putString("user", strUsername);
data.putString("sex", strSex);
data.putString("age", strAge);
data.putString("tell", strTell);
data.putString("email", strEmali);
data.putString("homepage", strHomepage);
data.putString("note", strNote);
//通过意图携带数据包
intent.putExtras(data);
//按照意图启动目标组件
startActivity(intent);
}else {
Toast.makeText(MainActivity.this,"注册失败",Toast.LENGTH_LONG).show();
}
}
});
// 给取消按钮添加监听器,实现监听器接口,编号事件处理方法
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
Activity类main.xml——代码
<?xml version="1.0" encoding="utf-8"?>
<EditText
android:id="@+id/edtusername"
android:layout_width="250dp"
android:layout_height="40dp"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sex"
android:textSize="30sp"
android:textColor="#c8f517e6"
android:paddingTop="20dp"/>
<EditText
android:id="@+id/edtsex"
android:layout_width="250dp"
android:layout_height="40dp"
android:ems="2" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/age"
android:textSize="30sp"
android:textColor="#c8f517e6"
android:paddingTop="20dp"/>
<EditText
android:id="@+id/edtage"
android:layout_width="250dp"
android:layout_height="40dp"
android:ems="2" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tell"
android:textSize="30sp"
android:textColor="#c8f517e6"
android:paddingTop="20dp"/>
<EditText
android:id="@+id/edttell"
android:layout_width="250dp"
android:layout_height="40dp"
android:ems="12" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/email"
android:textSize="30sp"
android:textColor="#c8f517e6"
android:paddingTop="20dp"/>
<EditText
android:id="@+id/edtemail"
android:layout_width="250dp"
android:layout_height="40dp"
android:ems="16" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/home_page"
android:textSize="30sp"
android:textColor="#c8f517e6"
android:paddingTop="20dp"/>
<EditText
android:id="@+id/homepage"
android:layout_width="250dp"
android:layout_height="40dp"
android:ems="20" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/note"
android:textSize="30dp"
android:textColor="#c8f517e6"
android:paddingTop="20dp"/>
<EditText
android:id="@+id/note"
android:layout_width="250dp"
android:layout_height="80dp"
android:ems="2" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="45dp">
<Button
android:id="@+id/btn1"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:text="@string/Registration"/>
<Button
android:id="@+id/btn2"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:text="@string/cancelled"/>
</LinearLayout>
java类LoginActivity——代码
package net.cgh.userlongin;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.TextView;
public class LoginActivity extends AppCompatActivity {
private TextView louser;
private TextView losex;
private TextView loage;
private TextView lotell;
private TextView loemali;
private TextView lohomepage;
private TextView lonote;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//利用布局资源文件设置用户界面
setContentView(R.layout.activity_login);
//通过资源标识获取控件实例
louser = (TextView) findViewById(R.id.lo_user);
losex = (TextView) findViewById(R.id.lo_sex);
loage = (TextView) findViewById(R.id.lo_age);
lotell = (TextView) findViewById(R.id.lo_tell);
loemali = (TextView) findViewById(R.id.lo_emali);
lohomepage = (TextView) findViewById(R.id.lo_homepage);
lonote = (TextView) findViewById(R.id.lo_note);
//获取意图
Intent intent =getIntent();
//判断是否为空
if(intent !=null){
//获取意图携带的数据包
Bundle data = intent.getExtras();
String strUsername = data.getString("user");
String strSex = data.getString("sex");
String strAge = data.getString("age");
String strTell = data.getString("tell");
String strEmail = data.getString("email");
String strHomepage = data.getString("homepage");
String strNote = data.getString("note");
//并接用户信息
String strUserlo="姓名:"+strUsername;
String strSexlo ="性别:"+strSex;
String strAgelo = "年龄:"+strAge;
String strtelllo ="电话:"+strTell;
String stremaillo = "邮箱;"+strEmail;
String strhomepage = "主页:"+strHomepage;
String strnote = "备注:"+strNote;
//设置标签文本属性,显示用户信息
louser.setText(strUserlo);
losex.setText(strSexlo);
loage.setText(strAgelo);
lotell.setText(strtelllo);
loemali.setText(stremaillo);
lohomepage.setText(strhomepage);
lonote.setText(strnote);
}
}
}
Activity类activity_login.xml——代码
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/lo_sex"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/lo_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/lo_tell"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/lo_emali"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/lo_homepage"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/lo_note"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>