码迷,mamicode.com
首页 > 其他好文 > 详细

随手记(二)

时间:2017-06-07 00:39:28      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:rtu   sch   cat   ase   ntop   userdata   fill   nis   div   

首先一个随手记app需要一个登陆注册功能,而androidstudio自带的sqllite就起到了很强大的作用。

1.用户登陆界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/login_layout">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="用户名:"
             />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入用户名"
            android:id="@+id/count" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="密    码:"
         />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:id="@+id/pwd"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登录"
            android:layout_marginRight="100sp"
            android:id="@+id/login" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="注册"
            android:id="@+id/regin" />
</LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="登录状态:"
       />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/state" />
</LinearLayout>

技术分享

2.用户登录activity

public class LoginActivity extends AppCompatActivity {
    private Button reg;
    private Button login;
    private EditText count;
    private EditText pwd;
    private TextView state;
    private List<User> userList;
    private List<User> dataList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        reg= (Button) findViewById(R.id.regin);
        login= (Button) findViewById(R.id.login);
        count= (EditText) findViewById(R.id.count);
        pwd= (EditText) findViewById(R.id.pwd);
        state= (TextView) findViewById(R.id.state);
        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=count.getText().toString().trim();
                String pass=pwd.getText().toString().trim();
                User user=new User();
                user.setUsername(name);
                user.setUserpwd(pass);
                int result=SqliteDB.getInstance(getApplicationContext()).saveUser(user);
                if (result==1){
                    state.setText("注册成功!");
                }else  if (result==-1)
                {
                    state.setText("用户名已经存在!");
                }
                else
                {
                    state.setText("!");
                }

            }
        });
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=count.getText().toString().trim();
                String pass=pwd.getText().toString().trim();
                int result=SqliteDB.getInstance(getApplicationContext()).Quer(pass,name);
                if (result==1)
                {
                        state.setText("登录成功!");
                }
                else if (result==0){
                    state.setText("用户名不存在!");

                }
                else if(result==-1)
                {
                    state.setText("密码错误!");
                }
            }
        });
    }
}

3.用户注册主界面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:weightSum="1">

    <EditText
        android:drawableLeft="@android:drawable/ic_menu_myplaces"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/resetpwd_edit_name"
        android:layout_alignParentTop="true"
        android:hint="请输入您的用户名"
        android:layout_alignLeft="@+id/resetpwd_edit_pwd_new"
        android:layout_alignStart="@+id/resetpwd_edit_pwd_new"
        android:layout_alignRight="@+id/resetpwd_edit_pwd_new"
        android:layout_alignEnd="@+id/resetpwd_edit_pwd_new" />
    
    <EditText
        android:drawableLeft="@android:drawable/ic_lock_idle_lock"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/resetpwd_edit_pwd_old"
        android:hint="请输入您的密码"
        android:layout_below="@+id/resetpwd_edit_name"
        android:layout_alignRight="@+id/resetpwd_edit_name"
        android:layout_alignEnd="@+id/resetpwd_edit_name"
        android:layout_alignLeft="@+id/resetpwd_edit_name"
        android:layout_alignStart="@+id/resetpwd_edit_name" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="取消"
        android:id="@+id/register_btn_cancel"
        android:textSize="20dp"
        android:background="#f71818"
        android:layout_below="@+id/register_btn_sure"
        android:layout_alignLeft="@+id/register_btn_sure"
        android:layout_alignStart="@+id/register_btn_sure"
        android:layout_marginTop="10dp" />

    <EditText
        android:drawableLeft="@android:drawable/ic_lock_idle_lock"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/resetpwd_edit_pwd_new"
        android:layout_below="@+id/resetpwd_edit_pwd_old"
        android:layout_centerHorizontal="true"
        android:hint="请确认您的密码" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="确定"
        android:id="@+id/register_btn_sure"
        android:textSize="20dp"
        android:background="#1cf718"
        android:layout_below="@+id/resetpwd_edit_pwd_new"
        android:layout_alignLeft="@+id/resetpwd_edit_pwd_new"
        android:layout_alignStart="@+id/resetpwd_edit_pwd_new"
        android:layout_marginTop="20dp" />

</RelativeLayout>

4.用户注册activity

public class Register extends AppCompatActivity {
    private EditText mAccount;                        
    private EditText mPwd;                          
    private EditText mPwdCheck;                      
    private Button mSureButton;                       
    private Button mCancelButton;                    
    private UserDataManager mUserDataManager;         
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        mAccount = (EditText) findViewById(R.id.resetpwd_edit_name);
        mPwd = (EditText) findViewById(R.id.resetpwd_edit_pwd_old);
        mPwdCheck = (EditText) findViewById(R.id.resetpwd_edit_pwd_new);

        mSureButton = (Button) findViewById(R.id.register_btn_sure);
        mCancelButton = (Button) findViewById(R.id.register_btn_cancel);

        mSureButton.setOnClickListener(m_register_Listener);      //注册界面两个按钮的监听事件
        mCancelButton.setOnClickListener(m_register_Listener);
        if (mUserDataManager == null) {
            mUserDataManager = new UserDataManager(this);
            mUserDataManager.openDataBase();                              
        }
    }
    View.OnClickListener m_register_Listener = new View.OnClickListener() {    //不同按钮按下的监听事件选择
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.register_btn_sure:                       //确认按钮的监听事件
                    register_check();
                    break;
                case R.id.register_btn_cancel:                     //取消按钮的监听事件,由注册界面返回登录界面
                    Intent intent_Register_to_Login = new Intent(Register.this,Login.class) ;    //切换User Activity至Login Activity
                    startActivity(intent_Register_to_Login);
                    finish();
                    break;
            }
        }
    };
    public void register_check() {                                //确认按钮的监听事件
        if (isUserNameAndPwdValid()) {
            String userName = mAccount.getText().toString().trim();
            String userPwd = mPwd.getText().toString().trim();
            String userPwdCheck = mPwdCheck.getText().toString().trim();
            //检查用户是否存在
            int count=mUserDataManager.findUserByName(userName);
            //用户已经存在时返回,给出提示文字
            if(count>0){
                Toast.makeText(this, getString(R.string.name_already_exist, userName),Toast.LENGTH_SHORT).show();
                return ;
            }
            if(userPwd.equals(userPwdCheck)==false){     //两次密码输入不一样
                Toast.makeText(this, getString(R.string.pwd_not_the_same),Toast.LENGTH_SHORT).show();
                return ;
            } else {
                UserData mUser = new UserData(userName, userPwd);
                mUserDataManager.openDataBase();
                long flag = mUserDataManager.insertUserData(mUser); //新建用户信息
                if (flag == -1) {
                    Toast.makeText(this, getString(R.string.register_fail),Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(this, getString(R.string.register_success),Toast.LENGTH_SHORT).show();
                    Intent intent_Register_to_Login = new Intent(Register.this,Login.class) ;    //切换User Activity至Login Activity
                    startActivity(intent_Register_to_Login);
                    finish();
                }
            }
        }
    }
    public boolean isUserNameAndPwdValid() {
        if (mAccount.getText().toString().trim().equals("")) {
            Toast.makeText(this, getString(R.string.account_empty),
                    Toast.LENGTH_SHORT).show();
            return false;
        } else if (mPwd.getText().toString().trim().equals("")) {
            Toast.makeText(this, getString(R.string.pwd_empty),
                    Toast.LENGTH_SHORT).show();
            return false;
        }else if(mPwdCheck.getText().toString().trim().equals("")) {
            Toast.makeText(this, getString(R.string.pwd_check_empty),
                    Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}

 

随手记(二)

标签:rtu   sch   cat   ase   ntop   userdata   fill   nis   div   

原文地址:http://www.cnblogs.com/sailyw/p/6954221.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!