在输入账号密码后登录和重置两个按钮都不起作用
代码如下:
mainactivity。java
package com.easybooks.myapplication15; import android.content.Intent; import android.os.Bundle; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.snackbar.Snackbar; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import android.os.Handler; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.TextView; import android.widget.TimePicker; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private EditText myName; //“用户名”输入框 private EditText myPwd; //“密码”输入框 private Button myOk; //“登录”按钮 private TimePicker myTime; //时间控件 private DatePicker myDate; //日期控件 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViews(); } private void findViews() { myName = findViewById(R.id.myTextName); myPwd = findViewById(R.id.myTextPwd); myOk = findViewById(R.id.myButtonOk); myTime = findViewById(R.id.myTimePicker); myDate = findViewById(R.id.myDatePicker); myTime.setIs24HourView(true); } public void onLoginClick(View view) { String name = myName.getText().toString(); //获得用户名 String pass = myPwd.getText().toString(); //获得密码 int year, month, day, hour, minute; year = myDate.getYear(); //获得日期中的年 month = myDate.getMonth() + 1; //获得日期中的月 day = myDate.getDayOfMonth(); //获得日期中的日 hour = myTime.getCurrentHour(); //获得日期中的时 minute = myTime.getCurrentMinute(); //获得日期中的分 //(a)定义Intent数据传递机制 Intent intent = new Intent(this, MainActivity2.class); Bundle bundle = new Bundle(); //定义Bundle对象bundle bundle.putString("name", name); //字符串name内容绑定到bundle bundle.putString("pass", pass); bundle.putInt("year", year); bundle.putInt("month", month); bundle.putInt("day", day); bundle.putInt("hour", hour); bundle.putInt("minute", minute); intent.putExtras(bundle); //bundle放入Intent传输机制 //(b)启动欢迎页面的Activity //startActivity(intent); startActivityForResult(intent, 200); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == 101) Toast.makeText(this, "重新登录", Toast.LENGTH_LONG).show(); else if (resultCode == 404) { Toast.makeText(this, "此前输入" + data.getStringExtra("pass") + "密码错!\n请重输", Toast.LENGTH_LONG).show(); myPwd.setText(""); //清空密码框 } } }
mainactivity2.java
package com.easybooks.myapplication15; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity2 extends AppCompatActivity { private TextView myWelcome; //文本视图(显示欢迎信息) private TextView myTime; //文本视图(显示登录时间) private String name, pass; //存储登录页传来的用户名密码 private int resultCode; //响应码:登录成功101; 错误404 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); //设置显示页面为welcome.xml findViews(); showWelcome(); } private void findViews() { myWelcome = findViewById(R.id.myLabelWelcome); myTime = findViewById(R.id.myLabelTime); } private void showWelcome() { Bundle bundle = getIntent().getExtras(); //定义Bundle对象bundle name = bundle.getString("name"); //从bundle中得到name内容(输入的用户名) pass = bundle.getString("pass"); //从bundle中得到pass内容(输入的密码) if (pass.equals("123456")) { //验证密码 myWelcome.setText("\n" + name + " 您好!\n 欢迎光临"); int year, month, day, hour, minute; //显示欢迎信息 year = bundle.getInt("year"); month = bundle.getInt("month"); day = bundle.getInt("day"); hour = bundle.getInt("hour"); minute = bundle.getInt("minute"); myTime.setText("登录时间:" + year + "-" + month + "-" + day + " " + hour + ":" + minute); resultCode = 101; //显示登录时间,返回成功码101 } else { myWelcome.setText("密码错!"); resultCode = 404; //返回错误码404 } //这里定义的Intent机制是为了向登录页面返回响应码及处理的结果 Intent data = new Intent(this, MainActivity.class); data.putExtras(bundle); setResult(resultCode, data); } public void onBackClick(View view) { finish(); //结束当前Activity,回到前一个Activity } }