package net.micode.notes.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import net.micode.notes.R;


public class signinActivity extends Activity {

    private Button sure ;

    private Button cancel ;

    private EditText myusername ;

    private EditText mypassword ;
//
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signin);

        sure = (Button) findViewById(R.id.oksignin);
        cancel = (Button) findViewById(R.id.cancelsignin);
        myusername = (EditText) findViewById(R.id.usersigin);
        mypassword = (EditText) findViewById(R.id.passwordsigin1);

        setListener();
    }

    private void setListener(){
        onClick onClick = new onClick() ;
        sure.setOnClickListener(onClick);
        cancel.setOnClickListener(onClick);
    }

    public class onClick implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.oksignin:
                    String username = myusername.getText().toString() ;
                    String password = mypassword.getText().toString() ;
                    String ok = "注册成功" ;
                    String fail = "注册失败" ;
                    SharedPreferences preferences = getSharedPreferences("user_info", MODE_PRIVATE);
                    SharedPreferences.Editor editor = preferences.edit();
                    String savedUsername = preferences.getString("username", "");
                    if(true ){
                        Toast toastCenter = Toast.makeText(getApplicationContext(), ok , Toast.LENGTH_SHORT);
                        toastCenter.setGravity(Gravity.CENTER , 0 , 0);
                        toastCenter.show();
                        editor.putString("username", username); // 将用户名存储到SharedPreferences中
                        editor.putString("password", password); // 将密码存储到SharedPreferences中
                        editor.apply(); // 提交修改
                        Intent intent = new Intent(signinActivity.this,loginActivity.class) ;
                        startActivity(intent);
                    }else{
                        Toast toastCenter =  Toast.makeText(getApplicationContext(), fail , Toast.LENGTH_SHORT);
                        toastCenter.setGravity(Gravity.CENTER , 0 , 0);
                        toastCenter.show();
                    }
                    break;
                case R.id.cancelsignin:
                    Intent intent = new Intent(signinActivity.this,loginActivity.class) ;
                    startActivity(intent);
                    break;
            }
        }
    }
}
