交互软件的登录界面

和几个同学一起组成的小组,准备做一个交互软件,因为水平不高所以程序比较一般,第一个是登录界面。

//继承AppCompatActivity的登录界面

public class LoginActivity extends AppCompatActivity implements View.OnClickListener
{
    private EditText editText1,editText2;
    private CheckBox checkBox1,checkBox2;
    private Button button1,button2;
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        //要显示的布局
        setContentView(R.layout.activity_login);
        // 通知栏透明,并且取消项目标题(沉浸式)
        getSupportActionBar().hide();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //初始化
        iniv();

        //存储数据

        SharedPreferences sharedPreferences = getSharedPreferences("text", Activity.MODE_PRIVATE);

        //获取两个单选框的状态,是否被勾选。

        boolean a = sharedPreferences.getBoolean("checked1",false);//记住账号密码

        boolean b = sharedPreferences.getBoolean("checked2",false);//自动登录

        //如果上次登录a单选框时处于勾选状态,那将上次所存储的账号密码赋值给输入框

        if (a)
        {
            editText1.setText(sharedPreferences.getString("accounts",""));
            editText2.setText(sharedPreferences.getString("password",""));
        }

        //如果上次登录b单选框时处于勾选状态,那将处于默认点击登录按钮的状态

       if (b)
        {
            button1.performClick();
        }

        //给控件设点击监听事件

         private void iniv()
    {
        editText1 = (EditText) findViewById(R.id.edit1);
        editText2 = (EditText) findViewById(R.id.edit2);
        checkBox1 = (CheckBox) findViewById(R.id.checkbox1);
        checkBox2 = (CheckBox) findViewById(R.id.checkbox2);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);


        button1.setOnClickListener(this);
        button2.setOnClickListener(this);


    }

     @Override
    public void onClick(View view)
    {

         //获取控件id进行判断
        switch (view.getId())
        {
            case R.id.button1:
                String accounts = editText1.getText().toString();
                String password = editText2.getText().toString();
                //进行登录操作
                login(accounts,password);


                break;
            case R.id.button2:
                //跳转到登录界面
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
                break;
        }
    }

     //因为没有自己创建服务器,所以这里用网易的服务器,所以这里用的方法是网易服务器的sdk的方法

     private void login(String accounts,String password)
    {
        //使用网易云信的SDK进行登入操作
        LoginInfo loginInfo = new LoginInfo(accounts,password);
        RequestCallback<LoginInfo> callback = new RequestCallback<LoginInfo>()
        {
            @Override
            public void onSuccess(LoginInfo loginInfo)//登录成功后的操作
            {
//                登录成功后跳转到主界面
                startActivity(new Intent(LoginActivity.this,MainActivity.class));
                //如果记住密码框被勾选
                if (checkBox1.isChecked())
                {
                    //存储登录帐号和密码
                    SharedPreferences sharedPreferences = getSharedPreferences("text",Activity.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("accounts",loginInfo.getAccount());
                    editor.putString("password",loginInfo.getToken());
                    editor.apply();
                }
                //把登录帐号和密码给用户信息类
                UserData.accounts = loginInfo.getAccount();
                UserData.password = loginInfo.getToken();


                Toast.makeText(LoginActivity.this, "登入成功!", Toast.LENGTH_SHORT).show();
                //销毁当前ACtivity
                finish();
            }


            @Override
            public void onFailed(int i)//登录失败后的操作
            {
                if (i == 302 || i == 404)
                {
                    Toast.makeText(LoginActivity.this, "帐号或密码错误", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(LoginActivity.this, "登录失败: " + i, Toast.LENGTH_SHORT).show();
                }
            }


            @Override
            public void onException(Throwable throwable)//登录异常的操作
            {
                Toast.makeText(LoginActivity.this, "无效输入", Toast.LENGTH_LONG).show();
            }
        };
        NIMClient.getService(AuthService.class).login(loginInfo).setCallback(callback);


        //不管有没有登录成功都记着当前的勾选状态
        SharedPreferences sharedPreferences = getSharedPreferences("text",Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("checked1",checkBox1.isChecked());
        editor.putBoolean("checked2",checkBox2.isChecked());
        editor.apply();
    }
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值