1.MainActivity.java
1 package com.example.administrator.myapplication; 2 3 import android.content.Intent; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.EditText; 8 import android.widget.Toast; 9 10 public class MainActivity extends AppCompatActivity { 11 EditText et_usercode; 12 EditText et_password; 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 18 et_usercode=(EditText)findViewById(R.id.et_usercode); 19 et_password=(EditText)findViewById(R.id.et_password); 20 } 21 //view 代表事件发起者 22 public void bt1_onclick(View v) 23 { 24 //带返回的打开 注册Acivity 25 26 //第一步:构造意图 27 28 Intent intent = new Intent(this,zhuceActivity.class); 29 30 startActivityForResult(intent,1); 31 } 32 //成员变量 33 String usercode; 34 String username; 35 String password; 36 37 //重写处理返回信息的回调方法 38 39 @Override 40 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 41 super.onActivityResult(requestCode, resultCode, data); 42 43 //处理返回信息 44 //1-判断请求码 45 if (requestCode==1) 46 { 47 //2-判断结果码 48 if (resultCode==RESULT_OK) 49 { 50 //接收返回的注册信息 51 usercode = data.getStringExtra("usercode"); 52 username = data.getStringExtra("username"); 53 password = data.getStringExtra("userpassword"); 54 } 55 56 } 57 58 } 59 60 //登录的方法 61 62 public void bt2_onclick(View v) 63 { 64 //1.取得填写信息 65 String uc = et_usercode.getText().toString(); 66 String pw = et_password.getText().toString(); 67 68 // 2.判断是否正确填写 69 if (uc.trim().length()==0||pw.trim().length()==0) 70 { 71 Toast.makeText(MainActivity.this, "用户代码和密码不能为空", Toast.LENGTH_LONG).show(); 72 73 return; 74 } 75 // 3.判断有没有注册信息 76 //1-没有找到注册信息 2-填写的用户信息尚未注册 77 if (usercode ==null||(usercode !=null && !usercode.equals(uc))) 78 { 79 Toast.makeText(MainActivity.this, "用户未注册", Toast.LENGTH_LONG).show(); 80 81 return; 82 } 83 // 4.注册信息与登录信息是否匹配 84 85 if (!password.equals(pw)) 86 { 87 Toast.makeText(MainActivity.this, "密码错误", Toast.LENGTH_LONG).show(); 88 89 return; 90 } 91 else 92 { 93 //可以登录系统 94 Toast.makeText(MainActivity.this, "用户验证成功", Toast.LENGTH_LONG).show(); 95 96 //跳转到主界面 97 startActivity(new Intent(this,TestActivity.class)); 98 } 99 } 100 }
2.Actvitymain.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.example.administrator.myapplication.MainActivity" 11 android:orientation="vertical"> 12 13 <EditText 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:hint="用户代码" 17 android:id="@+id/et_usercode"/> 18 <EditText 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:hint="密码" 22 android:id="@+id/et_password" 23 android:inputType="textPassword"/> 24 <LinearLayout 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content"> 27 <Button 28 android:layout_width="0dp" 29 android:layout_height="wrap_content" 30 android:layout_weight="1" 31 android:text="登录" 32 android:onClick="bt2_onclick"/> 33 <Button 34 android:layout_width="0dp" 35 android:layout_height="wrap_content" 36 android:layout_weight="1" 37 android:text="注册" 38 android:onClick="bt1_onclick"/> 39 </LinearLayout> 40 </LinearLayout>
3.zhuceActivity.xml
1 package com.example.administrator.myapplication; 2 3 import android.content.Intent; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.EditText; 8 import android.widget.Toast; 9 10 public class zhuceActivity extends AppCompatActivity { 11 EditText et_usercode1; 12 EditText et_password1; 13 EditText et_username1; 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_zhuce); 18 19 et_usercode1 = (EditText)findViewById(R.id.et_usercode_1); 20 et_password1 = (EditText)findViewById(R.id.et_password_1); 21 et_username1 = (EditText)findViewById(R.id.et_username_1); 22 } 23 //view 代表事件发起 24 public void bt1_onclick(View v) 25 { 26 //返回注册信息 27 //用户代码 28 String usercode = et_usercode1.getText().toString(); 29 if(usercode==null||usercode.trim().length()==0) 30 { 31 Toast.makeText(zhuceActivity.this, "请正确填写用户代码", Toast.LENGTH_LONG).show(); 32 33 return; 34 } 35 String username = et_username1.getText().toString(); 36 if(username==null||username.trim().length()==0) 37 { 38 Toast.makeText(zhuceActivity.this, "请正确填写用户名称", Toast.LENGTH_LONG).show(); 39 40 return; 41 } 42 String password = et_password1.getText().toString(); 43 if(password==null||password.trim().length()==0) 44 { 45 Toast.makeText(zhuceActivity.this, "请正确填写用户密码", Toast.LENGTH_LONG).show(); 46 47 return; 48 } 49 50 Intent intent = new Intent(); 51 52 intent.putExtra("usercode",usercode); 53 intent.putExtra("username",username); 54 intent.putExtra("password",password); 55 56 //设置返回信息:1-结果码;2-携带数据的Intent. 57 setResult(RESULT_OK,intent); 58 59 finish(); 60 } 61 62 public void bt2_onclick(View v) 63 { 64 setResult(RESULT_CANCELED, null); 65 66 finish(); 67 } 68 }
4.activity_zhuce.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.example.administrator.myapplication.zhuceActivity"> 11 <EditText 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 android:hint="用户代码" 15 android:id="@+id/et_usercode_1"/> 16 <EditText 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:hint="用户名称" 20 android:id="@+id/et_username_1" 21 android:layout_below="@+id/et_usercode_1"/> 22 <EditText 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content" 25 android:hint="登录密码" 26 android:id="@+id/et_password_1" 27 android:layout_below="@+id/et_username_1" 28 android:inputType="textPassword"/> 29 <LinearLayout 30 android:layout_width="match_parent" 31 android:layout_height="wrap_content" 32 android:layout_alignParentBottom="true"> 33 <Button 34 android:layout_width="0dp" 35 android:layout_height="wrap_content" 36 android:layout_weight="1" 37 android:text="取消" 38 android:onClick="bt2_onclick"/> 39 <Button 40 android:layout_width="0dp" 41 android:layout_height="wrap_content" 42 android:layout_weight="1" 43 android:text="确定" 44 android:onClick="bt1_onclick"/> 45 </LinearLayout> 46 </RelativeLayout>
呵呵,导致程序无法运行的错误啊!