app注册和第一次登录后自动登录,但是没有退出功能,待修改

<?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"
    tools:context="com.example.auto1.MainActivity"
    android:orientation="vertical"
    android:background="#F0F0F0"
    android:layout_weight="2">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.95"
        android:orientation="vertical"
        android:gravity="center">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="用户名"
                android:textSize="25dp"
                android:background="#FFFFFF"/>
            <EditText
                android:id="@+id/etName"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:textSize="25dp"
                android:background="#FFFFFF"/>
        </LinearLayout>
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#D4D4D4"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="密    码"
                android:textSize="25dp"
                android:background="#FFFFFF"/>
            <EditText
                android:id="@+id/etPwd"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:textSize="25dp"
                android:background="#FFFFFF"/>
        </LinearLayout>
        <Button
            android:id="@+id/btLogin"
            android:layout_marginTop="12dp"
            android:layout_width="230dp"
            android:layout_height="wrap_content"
            android:text="登录"
            android:textSize="20dp"
            android:background="#A2D7F5" />
    </LinearLayout>
    <RelativeLayout
        android:layout_weight="0.05"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/newUser"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="新用户"
            android:textColor="#A2D7F5"
            android:background="#F0F0F0"
            android:layout_alignParentRight="true"
            android:onClick="clickNewUser"/>
        <Button
            android:id="@+id/btQuestion"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="不能登录?"
            android:background="#F0F0F0"
            android:textColor="#A2D7F5"
             />
    </RelativeLayout>

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1">
    <TextView
        android:gravity="center"
        android:layout_weight="0.1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="QQ注册"
        android:textSize="25dp"
        android:textColor="#FFFFFF"
        android:background="#9AD3F4"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="昵称"
            android:textSize="25dp"/>
        <EditText
            android:id="@+id/etNameRes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码"
            android:textSize="25dp"/>
        <EditText
            android:id="@+id/etPwdRes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25dp"/>
    </LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码由6-16位数字、字母或符号组成,不能是9位以下纯数字"
        android:textSize="25dp"/>
    <Button
        android:id="@+id/btZhuce"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注册"
        android:textSize="20dp" />
</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登陆成功"/>
</LinearLayout>

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView etName;
    private TextView etPd;
    private Intent intent;
    private SharedPreferences Qqdata;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etName = (TextView) findViewById(R.id.etName);
        etPd = (TextView) findViewById(R.id.etPwd);

        Qqdata = getSharedPreferences("Qqdata", Context.MODE_PRIVATE);



        Intent intent1 = getIntent();
        boolean flagSp = intent1.getBooleanExtra("flagSp", false);
        boolean flagZhuce = intent1.getBooleanExtra("flagZhuce", false);

        if (flagZhuce) {
            etName.setText(intent1.getStringExtra("nameIntent"));
            etPd.setText(intent1.getStringExtra("pwdIntent"));
        }
        if (!flagSp) {
            autoLogin();
        }
        Button btLogin = (Button) findViewById(R.id.btLogin);
        btLogin.setOnClickListener(this);

        Button mewUser = (Button) findViewById(R.id.newUser);
        mewUser.setOnClickListener(this);

        Button btQuestion = (Button) findViewById(R.id.btQuestion);
        btQuestion.setOnClickListener(this);

    }
    public void autoLogin() {
        if(!Qqdata.getString("nameSp","").isEmpty() && !Qqdata.getString("pwdSp","").isEmpty()) {
            intent = new Intent(this,Deng.class);
            startActivity(intent);
            finish();
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btLogin:

                String name = etName.getText().toString();
                String password = etPd.getText().toString();
                Toast.makeText(this,name,Toast.LENGTH_SHORT).show();
                Toast.makeText(this,password,Toast.LENGTH_SHORT).show();
                if(!name.isEmpty() && !password.isEmpty()) {
                    if(name.equals( Qqdata.getString("nameSp","")) && password.equals(Qqdata.getString("pwdSp",""))) {
                        Toast.makeText(this,"进入比较",Toast.LENGTH_SHORT).show();
                        intent = new Intent(this,Deng.class);
                        startActivity(intent);
                        finish();
                    }else {
                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        builder.setIcon(R.mipmap.ic_launcher);
                        builder.setTitle("友情提示");
                        builder.setMessage("用户名或密码错误");
                        builder.setPositiveButton("确定",null);
                        builder.create().show();
                    }
                }
                break;
            case R.id.newUser:
                intent = new Intent(this,ZhuCe.class);
                startActivity(intent);
                finish();
                break;
        }
    }
}

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * Created by think on 2016/5/23.
 */
public class ZhuCe extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuce);

        final EditText etNameRes = (EditText) findViewById(R.id.etNameRes);
        final EditText etPwdRes = (EditText) findViewById(R.id.etPwdRes);

        Button btZhuce = (Button) findViewById(R.id.btZhuce);
        btZhuce.setOnClickListener(new View.OnClickListener() {

            private boolean flagSp = false;
            private boolean flagZhuce = false;
            @Override
            public void onClick(View v) {
                final String name = etNameRes.getText().toString();
                final String password = etPwdRes.getText().toString();
                SharedPreferences Qqdata = getSharedPreferences("Qqdata", Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = Qqdata.edit();
                edit.putString("nameSp",name);
                edit.putString("pwdSp",password);
                edit.commit();
                flagSp = true;

                AlertDialog.Builder builder = new AlertDialog.Builder(ZhuCe.this);
                builder.setIcon(R.mipmap.ic_launcher);
                builder.setTitle("友情提示");
                builder.setMessage("注册成功是否要自动登录");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(ZhuCe.this,MainActivity.class);
                        intent.putExtra("nameIntent",name);
                        intent.putExtra("pwdIntent",password);
                        flagZhuce = true;
                        intent.putExtra("flagSp",flagSp);
                        intent.putExtra("flagZhuce",flagZhuce);
                        startActivity(intent);
                        finish();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        finish();
                    }
                });
                builder.create().show();
            }
        });
    }
}

import android.app.Activity;
import android.os.Bundle;

/**
 * Created by think on 2016/5/23.
 */
public class Deng extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deng);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值