安卓登录/注册示例项目

4
是否有一个参考示例项目可用于在Android中实现登录/注册欢迎屏幕?
我已经考虑了需要知道的内容:
- 活动布局 - 文本字段验证 - 文本字段提示 - 启用/禁用UI元素(即仅在输入数据后按下登录) - 异步网络请求验证凭据 - 阻止UI等待登录 - 根据登录结果转发到下一个活动 - 如果是第一次使用,则进行注册插曲 - 如果用户希望取消网络登录(如果用户从活动中移开,不要在回调上崩溃) - 保存用户数据,访问令牌(如果有)或密码(并加密它) - 转换动画,包括“访问被拒绝”反馈 - 如果可用,使用缓存的凭据 - 登录“记住我”选择退出 - 等等。
还有很多要写的点,但你明白我的意思。
我怀疑,如果我错了,请纠正我,许多应用程序可能具有(或甚至开始于)登录/注册屏幕。 可能有一个自定义的Eclipse项目,捕获大多数最佳实践,以便开发人员不必重复造轮子?
显然,许多应用程序希望以不同的方式进行操作。 显然,对于某些情况,上述内容是无意义的。 但是...也许对于某些应用程序来说,这是一个合理的“典型”登录活动?
3个回答

11

Android开发工具(ADT)Eclipse插件的最新版本提供了一个创建新LoginActivity向导,可以作为一个很好的起点。它具有以下功能:

  • Activity布局文本字段验证
  • 文本字段提示
  • 启用/禁用UI元素
  • 异步请求验证凭据
  • 阻塞UI等待登录
  • 取消登录
  • 等等。

它在文件 -> 新建 -> 其他 -> Android活动 -> LoginActivity中。


3

1) 在/res/layout中创建login.xml(活动布局)。在此示例中,还有一个额外的选项可用于更改密码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/username"
        android:hint="Username"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="180dp"
        android:layout_gravity="center"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/password"
        android:layout_width="250dp"
        android:hint="Password"
        android:layout_marginTop="20dp"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/Loginbutton"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login" />

    <CheckedTextView
        android:id="@+id/changepassword"
        android:layout_width="wrap_content"
        android:layout_marginTop="75dp"
        android:clickable="true"
        android:layout_marginLeft="190dp"
        android:layout_height="wrap_content"
        android:text="Change_Login_Id" />

</LinearLayout>

2) 在 /src/ 目录下创建 Login.java 类

public class Login extends Activity{
    private Button login;
    private EditText Username;
    private EditText Password;
    private CheckedTextView changeid;
    public SQLiteDatabase sampleDB;
    public String COLUMN_ID="_id";
    public String COLUMN1="username";
    public String COLUMN2="password";
    public String TABLE_NAME="Androdata";
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        login=(Button)findViewById(R.id.Loginbutton);
        Username=(EditText)findViewById(R.id.username);
        Password=(EditText)findViewById(R.id.password);
        changeid=(CheckedTextView)findViewById(R.id.changepassword);
        sampleDB =  this.openOrCreateDatabase(TABLE_NAME, MODE_PRIVATE, null);
        boolean x=init(TABLE_NAME);
        if(x==false)
        {
        createDB();
        insertDB();
        }
        changeid.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent myintent=new Intent("android.intent.action.DATABASE");
                startActivity(myintent);
            }
        });
        login.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int k=check();
                if(k==1)                
                {
                    Toast.makeText(Login.this, "Login Successful ...", Toast.LENGTH_SHORT).show();
                    Intent myintent=new Intent("android.intent.action.CHOICE");
                    startActivity(myintent);
                }
                else
                {
                    Username.setText("");
                    Password.setText("");
                    Toast.makeText(Login.this, "Authentication Failed...", Toast.LENGTH_SHORT).show();
                }   
            }
        });
    }
    public boolean init(String tableName)
    {
        Cursor cursor = sampleDB.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+tableName+"'", null);
        if(cursor!=null) {
            if(cursor.getCount()>0) {
                                cursor.close();
                return true;
            }
                        cursor.close();
        }
        return false;
    }
    private void insertDB() {
        sampleDB.execSQL("INSERT INTO " +
                TABLE_NAME +
                " Values ('1','Androviewer','viewer');");   
        System.out.println("Inserted data successfully....");
    }
    private void createDB() {
        sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                TABLE_NAME+ "(" + COLUMN_ID
                + " integer primary key autoincrement, " + COLUMN1
                + " text not null,"+ COLUMN2
                + " text not null);");
        System.out.println("Database created successfully....");
    }
    private int check() {
        String a=Username.getText().toString();
        String b=Password.getText().toString();
        // TODO Auto-generated method stub
        Cursor c = sampleDB.rawQuery("SELECT username, password FROM " +
                TABLE_NAME +
                " where _id=1", null);

        if (c != null ) {
            if  (c.moveToFirst()) {
                do {
                    String orgusername = c.getString(c.getColumnIndex("username"));
                    String orgpassword = c.getString(c.getColumnIndex("password"));
                    if(a.equals(orgusername)&&b.equals(orgpassword))
                    {
                        return 1;
                    }
                    else
                    {
                        return 0;
                    }
                }while (c.moveToNext());
            }
        }
        return 0;
    }
}

1
if u want a single time login u can use shared preferences or make a simple change in my db code .

1)u can set textfield hint in xml

android:hint="ur hint" 

2)Use Intent as i used to move forward from one activity to next.

Do u need any more

2
为什么有两个不同的答案?一个应该就足够了。;-) - Siddharth Lele

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接