<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hanqi.testapp1.MainActivity"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户代码"
android:id="@+id/et_usercode"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:id="@+id/et_password"
android:inputType="textPassword"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="登录"
android:onClick="bt2_OnClick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="注册"
android:onClick="bt1_OnClick"/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hanqi.testapp1.ZhuceActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户代码"
android:id="@+id/et_usercode_1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名称"
android:id="@+id/et_username_1"
android:layout_below="@id/et_usercode_1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="登录密码"
android:id="@+id/et_password_1"
android:layout_below="@id/et_username_1"
android:inputType="textPassword"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消"
android:onClick="bt2_OnClick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="确定"
android:onClick="bt1_OnClick"/>
</LinearLayout>
</RelativeLayout>
package com.hanqi.testapp1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class ZhuceActivity extends AppCompatActivity {
EditText et_usercode1;
EditText et_password1;
EditText et_username1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zhuce);
et_usercode1=(EditText)findViewById(R.id.et_usercode_1);
et_usercode1=(EditText)findViewById(R.id.et_password_1);
et_usercode1=(EditText)findViewById(R.id.et_username_1);
}
//view 代表事件发起者
public void bt1_OnClick(View v)
{
//返回注册信息
//用户代码
String usercode=et_usercode1.getText().toString();
if(usercode==null||usercode.trim().length()==0)
{
Toast.makeText(ZhuceActivity.this, "请正确填写用户代码", Toast.LENGTH_SHORT).show();
return;
}
String username=et_username1.getText().toString();
if(username==null||usercode.trim().length()==0)
{
Toast.makeText(ZhuceActivity.this, "请正确填写用户名称", Toast.LENGTH_SHORT).show();
return;
}
String password=et_password1.getText().toString();
if(password==null||usercode.trim().length()==0)
{
Toast.makeText(ZhuceActivity.this, "请正确填写用户密码", Toast.LENGTH_SHORT).show();
return;
}
Intent intent =new Intent();
intent.putExtra("usercode",usercode);
intent.putExtra("username",username);
intent.putExtra("password",password);
setResult(RESULT_OK,intent);
finish();
}
}
package com.hanqi.testapp1;
/**
* Created by Administrator on 2016/4/29.
*/
public class TestActivity {
}
package com.hanqi.testapp1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText et_usercode;
EditText et_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_usercode = (EditText) findViewById(R.id.et_usercode);
et_password = (EditText) findViewById(R.id.et_password);
}
//view 代表事件的发起者
public void bt1_OnClick(View v) {
//带返回的打开 注册的Activity
//1 构造意图
Intent intent = new Intent(this, ZhuceActivity.class);
startActivityForResult(intent, 1);
}
String usercode;
String username;
String password;
//重写处理返回信息的回调方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//处理返回信息
//判断请求码
if (resultCode == 1) {
//判断结构码
if (resultCode == RESULT_OK) {
//接收返回的注册信息
usercode = data.getStringExtra("usercode");
username = data.getStringExtra("username");
password = data.getStringExtra("password");
}
}
}
//登陆的方法
public void bt2_OnClick(View v) {
//1.取得填写信息
//2.判断是否正确填写
String uc = et_usercode.getText().toString();
String pw = et_password.getText().toString();
if (uc.trim().length() == 0 || pw.trim().length() == 0) {
Toast.makeText(MainActivity.this, "用户代码和密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
//3.判断有没有注册信息
//1)没有找到注册信息
//2)填写的用户尚未注册
if (usercode == null || (usercode != null && !usercode.equals(uc))) {
Toast.makeText(MainActivity.this, "用户未注册", Toast.LENGTH_SHORT).show();
return;
}
//4.判断登录信息和注册信息是否配
if (!password.equals(pw)) {
Toast.makeText(MainActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
return;
}
else
{
//可以登录系统了
Toast.makeText(MainActivity.this, "用户验证成功", Toast.LENGTH_SHORT).show();
//跳转到主界面
Intent intent=new Intent(this,TestActivity.class);
//传递数据
//把数据放到Intent里
intent.putExtra("usercode",usercode);
intent.putExtra("username",username);
startActivity(intent);
}
}
}