qq_40694481
qq_40694481
2017-12-02 05:43

我用shearpreferences写的登录和注册 ,这个点登陆没反应

  • 求修改

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class loginActivity extends Activity {
private EditText editusername;
private EditText editpasswoard;
private Button login;
private Button rejister;
private CheckBox rememberpass;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editusername=(EditText)findViewById(R.id.editusername);
    final EditText editpassword = (EditText)findViewById(R.id.editpassword);
    login = (Button) findViewById(R.id.login);  
    Button register = (Button) findViewById(R.id.register); 
    register.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {  
            // TODO Auto-generated method stub  
            Intent intent = new Intent(loginActivity.this, register.class);  
            startActivity(intent);  
        } 
    });

    login.setOnClickListener(new Button.OnClickListener() {  

                @Override  
                public void onClick(View v) {  
                    // TODO Auto-generated method stub  
                    String username = editusername.getText().toString();  
                    String password = editpassword.getText().toString();   
                    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());  
                    boolean isRemember = pref.getBoolean("remember_mima", false); 
                    if(editusername.equals(pref.getString("username", ""))&&editpassword.equals(pref.getString("mima", ""))){  
                        Editor editor = pref.edit();
                        if (rememberpass.isChecked()) { 
                            editor.putBoolean("remember_mima", true);
                            Intent intent = new Intent(loginActivity.this, zhujiemian.class);  
                            startActivity(intent);  
                        }  
                        else{  
                            Toast.makeText(loginActivity.this, "用户名或密码错误", Toast.LENGTH_LONG).show();
                            editor.clear();  
                        }  

                    }  

                }  
            });  
        }



@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;
}

}

//注册.java
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zy.R;

public class register extends Activity {
private EditText editusername;
private EditText editpasswoard;
private Button preservation;
String TAG = "loginActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);
    preservation = (Button) findViewById(R.id.preservation);
    editusername=(EditText)findViewById(R.id.editusername);
    final EditText mima= (EditText)findViewById(R.id.mima);
    preservation.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v){
            //获取SharedPreferences实例后,可以通过SharedPreferences.Editor类对SharedPreferences进行修改
            SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();  
            editor.putString("username", editusername.getText().toString());  
            editor.putString("password", mima.getText().toString()); 
          //最后调用commit()函数保存修改内容
            editor.commit(); 
            Toast.makeText(getApplicationContext(),"保存成功",Toast.LENGTH_SHORT).show();
          }
        });
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答