android 实现简单注册登陆公能

public class MainActivity extends ActionBarActivity {

private Button landed;
private Button login;
private EditText account;
private EditText password;
private RadioGroup radioGroup;

final static int TYPE_ADMIN = 100; // 管理者
final static int TYPE_USER = 101; // 用户
private int flag = TYPE_ADMIN; // 监听是哪种类型的用户登陆

MyHelper myHepler;
private SQLiteDatabase db;
Cursor cursor;

@Override
public void onCreate(Bundle savedInstanceState) {
    // requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myHepler = new MyHelper(MainActivity.this, "my", 1);

    DefauitData();
    landed = (Button) findViewById(R.id.landed);
    login = (Button) findViewById(R.id.login);
    account = (EditText) findViewById(R.id.account);
    password = (EditText) findViewById(R.id.password);
    radioGroup = (RadioGroup) findViewById(R.id.landed_user_type);

    landed.setOnClickListener(new BtnListener());
    login.setOnClickListener(new BtnListener());

    radioGroup.setOnCheckedChangeListener(new CheckChangeListener());

}

private class CheckChangeListener implements OnCheckedChangeListener {

    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub
        switch (checkedId) {
        case R.id.admin: // 管理员
            flag = TYPE_ADMIN;
            account.setInputType(InputType.TYPE_CLASS_TEXT);
            password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            account.setText("");
            password.setText("");
            break;
        case R.id.user: // 商品发布者
            flag = TYPE_USER;
            account.setInputType(InputType.TYPE_CLASS_TEXT);
            password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            account.setText("");
            password.setText("");
            break;
        }
    }

}

private class BtnListener implements OnClickListener {
    // Intent intent = new Intent();
    Intent intent = new Intent();

    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
        case R.id.landed:
            Landed();
            break;
        case R.id.login:
            startActivity(intent.setClass(MainActivity.this, user_register.class));
            break;
        }
    }

}

private void DefauitData() {
    db = myHepler.getWritableDatabase();
    cursor = db.query("admin", null, null, null, null, null, null);
    if (cursor.getCount() <= 0) {
        ContentValues values = new ContentValues();
        values.put("admin_account", "123456");
        values.put("admin_password", "123456");
        db.insert("admin", null, values);
    }
    cursor.close();
    db.close();
    myHepler.close();
}

private void Landed() {
    Intent intent = new Intent();
    db = myHepler.getWritableDatabase();
    switch (flag) {
    case TYPE_ADMIN:
        cursor = db.query("admin", null, null, null, null, null, null);
        cursor.moveToFirst();
        if (cursor.getString(cursor.getColumnIndex("admin_account")).equals(account.getText().toString()) && cursor
                .getString(cursor.getColumnIndex("admin_password")).equals(password.getText().toString())) {
            startActivity(intent.setClass(MainActivity.this, test.class));
            account.setText("");
            password.setText("");
        } else {
            Toast.makeText(getApplicationContext(), "管理员账号或密码错误,请重新输入", Toast.LENGTH_SHORT).show();
            account.setText("");
            password.setText("");
        }
        cursor.close();
        db.close();
        myHepler.close();
        break;
    case TYPE_USER:
        int temp = 0;
        cursor = db.query("publish", null, null, null, null, null, null);
        //cursor=db.rawQuery("select * from publish",null);
        System.out.println("oooooooooooo"+cursor.getCount());
        /*for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToPosition(i);
            if (cursor.getString(cursor.getColumnIndex("publisher_name")).equals(account.getText().toString())
                    && cursor.getString(cursor.getColumnIndex("user_psaaword"))
                            .equals(password.getText().toString())) {
                temp = 1;
                Bundle bundle = new Bundle();
                bundle.putString("account", account.getText().toString());
                bundle.putString("password", password.getText().toString());
                account.setText("");
                password.setText("");
                Intent intent2 = new Intent();
                intent2.setClass(MainActivity.this, test.class);
                intent2.putExtras(bundle);
                startActivity(intent2);
            }
        }*/
        for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            int nameColumn = cursor.getColumnIndex("publisher_name");
            int phoneColumn = cursor.getColumnIndex("publisher_phonenumber");
            String name = cursor.getString(nameColumn);
            String phoneNumber = cursor.getString(phoneColumn);
            System.out.println(name);
            System.out.println(phoneNumber);
        }
        if (temp == 0) {
            Toast.makeText(getApplicationContext(), "用户名或密码错误,请重新输入", Toast.LENGTH_SHORT).show();
            account.setText("");
            password.setText("");
        }
        cursor.close();
        db.close();
        myHepler.close();
        break;
    }

}

}

猜你喜欢

转载自blog.csdn.net/lichen_6398/article/details/71223370