2016-11-23 75 views
0

我正在嘗試構建登錄表單。這是我的密碼字段的xml:必須在鍵盤上按兩次鍵才能登錄

<android.support.design.widget.TextInputLayout 
     android:id="@+id/password" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="4dp" 
     app:errorEnabled="true"> 

     <android.support.design.widget.TextInputEditText 
      android:id="@+id/password_input" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="@string/password" 
      android:inputType="textPassword" 
      android:imeOptions="actionGo"/> 
    </android.support.design.widget.TextInputLayout> 

,並在那裏我聽着前進按鍵看起來像這樣的loginActivity:

@OnClick(R.id.password_input) 
public void Start() { 
    EditText editText = (EditText) findViewById(R.id.password_input); 
    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
     @Override 
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
      boolean handled = false; 
      if (actionId == EditorInfo.IME_ACTION_GO) { 
       logIn(); 
       handled = true; 
      } 
      return handled; 
     } 
    }); 
} 

當我按密碼字段鍵盤彈出,然後我有按兩次轉入鍵以調用logIn()函數。可能是什麼原因,我該如何解決這個問題?

回答

2

更改您條件onEditorAction這樣的:

EditText editText = (EditText) findViewById(R.id.password_input); 
    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
     @Override 
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 

      if (actionId == EditorInfo.IME_ACTION_GO) { 
       logIn(); 

      } 
      return false; 
     } 
    }); 

我已經創建了一個測試情況下,檢查這一個:

在你的XML佈局地址:

<EditText 
     android:id="@+id/edtInput" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:imeOptions="actionGo" 
     android:inputType="textPassword" /> 

並在您的活動/片段代碼檢查:

EditText edtInput = (EditText) findViewById(R.id.edtInput); 
     edtInput.setImeOptions(EditorInfo.IME_ACTION_GO); 
     edtInput.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
      @Override 
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
       if (actionId == EditorInfo.IME_ACTION_GO) { 
        Toast.makeText(MainActivity.this, " GO ", Toast.LENGTH_SHORT).show(); 
       } 
       return false; 
      } 
     }); 

現在運行並檢查它是否適用於我,它也應該爲你工作。

最後我得到了與我們正在做一個愚蠢的錯誤,該解決方案能否請您刪除

@OnClick(R.id.password_input) 
public void Start() { 

行代碼,如果沒有使用,這是未讓所有發生的問題。

直接添加以下代碼到你的活動的onCreate(),沒有必要給點擊事件的EditText:

EditText editText = (EditText) findViewById(R.id.password_input); 
    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
     @Override 
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
      if (actionId == EditorInfo.IME_ACTION_GO) { 
       logIn(); 

      } 
      return false; 
     } 
    }); 
+0

如果工作然後接受的答案。 –

+0

仍然必須按兩次Enter鍵登錄。我需要登錄操作發生在單擊。 –

+0

@Al Noman我必須讓它工作在一個單一的點擊,它已經發生,當我按Enter鍵兩次。 –

1

只要替換你的條件是這樣的。

if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) { 
     Log.e(TAG, "enter_key_pressed"); 
    } 
0
@OnClick(R.id.password_input) 
public void Start() { 
    if (doublePressed) { 
     //your intent or Operation 
      return; 
    } 
    this.doublePressed = true; 
    Toast.makeText(this, "Please click again to do」, Toast.LENGTH_SHORT).show(); 

    new Handler().postDelayed(new Runnable() { 

     @Override 
     public void run() { 
      doublePressed=false;      
     } 
    }, 2000); 
} 
0
@OnClick(R.id.password_input) 
public void Start() { 
EditText editText = (EditText) findViewById(R.id.password_input); 
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 

     if (actionId == EditorInfo.IME_ACTION_GO) { 
      logIn(); 
      return true; 
     } 
     return false; 
    } 
}); 
} 
+0

在第一次啓動應用程序時,仍然必須按2次Enter鍵。 –

+0

您的TextInputEditText出現問題,請將其更改爲EditText ... – Bhavnik

相關問題