0 0 0

安卓实训开发-笔记本之登录注册

ZAESKY
2020-1-27 3005

Android程序设计是计算机软件技术专业的集中实践性环节之一,是学习《Android程序设计》课程后进行的一次全面的综合练习。其目的在于加深对Android编程知识的理解,掌握使用Android编程的基本方法,提高运用程序设计解决的能力。

记事本APP主要功能如下:

1、  以列表的形式将记事标标题及时间显示在主界面上;

2、  通过菜单的方式实现记事本的添加、删除、修改、查询、查看;

3、  实现记事本数据库的备份和恢复功能;

4、  制作一个关于本程序界面功能;

5、  用户必须登录成功后,才能对记事本进行各项操作;

6、  制作一个记事本使用帮助功能。


登录界面的实现代码

package com.example.notebook;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/*----------------------------------登录界面------------------------------------*/
public class MainActivity extends Activity {
                 private Button loginButton,registerButton;
                 private EditText nameEditText,pwdEditText; 
    //登入界面主代码
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nameEditText=(EditText)findViewById(R.id.main_edtusername);
        pwdEditText=(EditText)findViewById(R.id.main_edtuserpwd);
        loginButton=(Button)findViewById(R.id.main_btnlogin); 
        /*----------------------------------登录按钮监听事件------------------------------------*/
        loginButton.setOnClickListener(new OnClickListener() {
                         public void onClick(View arg0) {
                                  // TODO Auto-generated method stub
                                  //调用数据库
                                  MydatabaseHelper mydatabaseHelper=new MydatabaseHelper(MainActivity.this);
                                  //调用数据库寻找游标的类
                                  Cursor personCursor=mydatabaseHelper.findCousor();
                                  //利用for循环来匹配所输入的账号和密码
                                  for (personCursor.moveToFirst(); !personCursor.isAfterLast(); personCursor.moveToNext()) {
                                          String nameString=personCursor.getString(0);
                                          String pwdString=personCursor.getString(1);       
                                          if (nameEditText.getText().toString().equals("")&&pwdEditText.getText().toString().equals("")) {
                                                  Toast.makeText(MainActivity.this, "用户名或密码不能为空!", Toast.LENGTH_LONG).show();
                                                  break;
                                          }
                                          if (!nameEditText.getText().toString().equals(nameString)&&!pwdEditText.getText().toString().equals(pwdString)) {
                                                  Toast.makeText(MainActivity.this, "用户名或密码错误,请重试!", Toast.LENGTH_LONG).show();
                                                  break;
                                          }
                                          else {
                                                  Toast.makeText(MainActivity.this, "登录成功!", Toast.LENGTH_LONG).show();
                                                  Intent intent=new Intent(MainActivity.this,NoteActivity.class);
                                                  startActivity(intent);
                                                  break;
                                          }
                                          }
                                  }
                 });
        
        /*----------------------------------注册按钮监听事件------------------------------------*/
        registerButton=(Button)findViewById(R.id.main_btnregister);
        registerButton.setOnClickListener(new OnClickListener() {
                         public void onClick(View arg0) {
                                  Intent intent = new Intent(MainActivity.this,RegisterActivity.class);
                                  startActivity(intent);
                         }
                 });
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}


注册界面实现代码

package com.example.notebook;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/*----------------------------------注册功能------------------------------------*/
public class RegisterActivity extends Activity {
        private Button registerButton;
        private EditText nameEditText,pwdEditText,cfpwdEditText;
        protected void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
                 setContentView(R.layout.activity_register);
                 registerButton=(Button)findViewById(R.id.register_btnregister);
                 nameEditText=(EditText)findViewById(R.id.register_edtusername);
                 pwdEditText=(EditText)findViewById(R.id.register_edtuserpwd);
                 cfpwdEditText=(EditText)findViewById(R.id.register_edtcfpwd);
                 /*----------------------------------注册按钮事件监听------------------------------------*/
                 registerButton.setOnClickListener(new OnClickListener() {
                         public void onClick(View arg0) {
                                  String nameString=nameEditText.getText().toString();
                                 String pwdString=pwdEditText.getText().toString();
                                  String cfpwdString=cfpwdEditText.getText().toString();
                                  MydatabaseHelper mydatabaseHelper=new MydatabaseHelper(RegisterActivity.this);
                                  if (nameString.equals("")||pwdString.equals("")||cfpwdString.equals("")) {
                                          Toast.makeText(RegisterActivity.this, "用户名或密码不能为空!", Toast.LENGTH_LONG).show();
                                  }
                                          else if (!pwdString.equals(cfpwdString)) {
                                                  Toast.makeText(RegisterActivity.this, "密码输入不一致!请重新输入!", Toast.LENGTH_LONG).show();
                                          }
                                          else {
                                                Toast.makeText(RegisterActivity.this, "注册成功!请登录!", Toast.LENGTH_LONG).show();
                                                  mydatabaseHelper.insert(nameString, cfpwdString);
                                                  Intent intent = new Intent(RegisterActivity.this,MainActivity.class);
                                                  startActivity(intent);
                                          }
                                  }
                 });
}
        public boolean onCreateOptionsMenu(Menu menu) {
                 // TODO Auto-generated method stub
                 return super.onCreateOptionsMenu(menu);
        }
}


最新回复 (0)

    暂无评论

请先登录后发表评论!

返回
请先登录后发表评论!