2011-05-09 56 views
4

我有典型的登录屏幕。我能够使用imeOptions来允许用户“标签”从一个字段到另一个字段和最后一个字段(密码)我有actionDone - 它只是关闭软键盘。理想情况下,我喜欢自动点击“登录”。有什么内置的吗?Android ime选项和自动按登录按钮

回答

1
public class Main extends Activity 
{ 
    private final static String USERNAME = "user1"; 
    private final static String PASSWORD = "12345678"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.page1); 

    final EditText usernameInput = (EditText) findViewById(R.id.username); 
    EditText passwordInput = (EditText) findViewById(R.id.password); 
    passwordInput.addTextChangedListener(new TextWatcher() 
    { 
     @Override 
     public void afterTextChanged(Editable input) 
     { 
     if (USERNAME.equals(usernameInput.getText().toString()) && PASSWORD.equals(input.toString())) 
     { 
      setContentView(R.layout.page2); 
     } 
     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) 
     { 

     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) 
     { 

     } 
    }); 
    } 
}

page1.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center"> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="username:" /> 
    <EditText 
     android:id="@+id/username" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="password:" /> 
    <EditText 
     android:id="@+id/password" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
</LinearLayout>

page2.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Login successful." /> 
</LinearLayout>
+1

这实际上并没有回答他的问题,这只是自动日志中,当正确的密码类型。这也会是一个很大的安全风险,因为如果它的短或拼写错误(但更长) – Mgamerz 2014-02-10 18:02:25