关于android登陆跳转界面

hi_han 2015-10-28 06:24:55
package com.example;

import android.app.Activity;
import android.content.Intent;
//import android.app.backup.SharedPreferencesBackupHelper;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
//import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends Activity {

private EditText userName, password;
private CheckBox rem_pw, auto_login;
private Button btn_login;
//private ImageButton btnQuit;
private String userNameValue,passwordValue;
private SharedPreferences sp;

final static int REQUEST=10;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去除标题
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//获得实例对象
sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);
userName = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.userpassword);
rem_pw = (CheckBox) findViewById(R.id.remember);
btn_login = (Button) findViewById(R.id.login);


//判断记住密码多选框的状态
if(sp.getBoolean("ISCHECK", false))
{
//设置默认是记录密码状态
rem_pw.setChecked(true);
userName.setText(sp.getString("USER_NAME", ""));
password.setText(sp.getString("PASSWORD", ""));
//判断自动登陆多选框状态
/*if(sp.getBoolean("AUTO_ISCHECK", false))
{
//设置默认是自动登录状态
auto_login.setChecked(true);
//跳转界面
Intent intent = new Intent(MainActivity.this,LogoActivity.class);
MainActivity.this.startActivity(intent);

}
} */

rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (rem_pw.isChecked()) {

System.out.println("记住密码已选中");
sp.edit().putBoolean("ISCHECK", true).commit();

}else {

System.out.println("记住密码没有选中");
sp.edit().putBoolean("ISCHECK", false).commit();

}

}
});


// 登录监听事件 现在默认为用户名为:liu 密码:123
btn_login.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
userNameValue = userName.getText().toString();
passwordValue = password.getText().toString();
if(userNameValue.equals("liu")&&passwordValue.equals("123"))
{
Toast.makeText(MainActivity.this,"登录成功", Toast.LENGTH_SHORT).show();
//登录成功和记住密码框为选中状态才保存用户信息
if(rem_pw.isChecked())
{
//记住用户名、密码、
Editor editor = sp.edit();
editor.putString("USER_NAME", userNameValue);
editor.putString("PASSWORD",passwordValue);
editor.commit();
}
//跳转界面
//Intent intent = new Intent(MainActivity.this,LogoActivity.class);
//startActivity(intent);
//finish();
Intent it=new Intent(MainActivity.this, LogoActivity.class);
//startActivity(it);
startActivityForResult(it,REQUEST);
}
else{

Toast.makeText(MainActivity.this,"用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show();
}
}

});

//监听记住密码多选框按钮事件
rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (rem_pw.isChecked()) {

System.out.println("记住密码已选中");
sp.edit().putBoolean("ISCHECK", true).commit();

}else {

System.out.println("记住密码没有选中");
sp.edit().putBoolean("ISCHECK", false).commit();

}

}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}


这是代码部分,不知道为什么当帐户和密码正确的时候点击登陆,就自动退出了,但是如果是错的帐户和密码,会弹出那句提示“用户名或密码错误,请重新登录”!求大神指教
...全文
362 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
水的川 2015-10-28
  • 打赏
  • 举报
回复
看log,打断点啊。。。
yunmenggyy 2015-10-28
  • 打赏
  • 举报
回复
看看Log日志,是不是空指针异常?

80,347

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧