开发者

Android imeOptions and auto-pressing Login button

I have typical login screen. I was able to use imeOptions to allow user "tab" from one field to another and on last field(password) I have actionDone - it just closes soft keyboard. Ideally, I开发者_StackOverflow中文版 like to click "Login" automatically. Is there anything built-in for that?


Add imeOptions to your password EditText and define the action programmatically.

XML:

            <EditText
                android:id="@+id/passordEditText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="Enter password"
                android:inputType="textPassword"
                android:imeOptions="actionSend"/>

Java:

EditText passwordET= v.findViewById(R.id.passwordEditText);
passwordET.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                        boolean mHandled = false;
                        if (actionId == EditorInfo.IME_ACTION_SEND) {
                            onClick(yourLogInMethod);
                            mHandled = true;
                        }
                        return mHandled;
                    }
                });


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜