2017-09-12 86 views
0

當在軟輸入上單擊完成時,我想將焦點從編輯文本字段更改爲提交按鈕。如何將焦點從上次編輯文本字段更改爲單擊軟輸入完成的按鈕?

我有下面的代碼:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:paddingTop="5sp"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Driver + Conductor :" 
     android:textSize="17sp" 
     android:textStyle="bold" /> 

    <EditText 
     android:id="@+id/bus_driver_above60" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_weight="1" 
     android:hint="Driver" 
     android:inputType="numberDecimal" 
     android:textAlignment="center" 
     android:textColor="#000" 
     android:nextFocusDown="@+id/bus_conductor_above60"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="2dp" 
     android:text="+" 
     android:textSize="17sp" 
     android:textStyle="bold" /> 

    <EditText 
     android:id="@+id/bus_conductor_above60" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="2dp" 
     android:layout_weight="1" 
     android:hint="Conductor" 
     android:inputType="numberDecimal" 
     android:textAlignment="center" 
     android:textColor="#000" 
     android:nextFocus="@+id/bus_above60_btn"/> 

</LinearLayout> 

<Button 
    android:id="@+id/bus_above60_btn" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:text="Calculate" 
    android:textSize="17sp" 
    android:textColor="#ffffff" 
    android:textStyle="bold" 
    android:background="#3498DB"/> 

在這裏的代碼,

android:nextFocusDown="@+id/bus_conductor_above60" 

改變從驅動編輯文本字段焦點導體編輯文本字段。

但是在編輯文本欄後,有一個計算按鈕 - bus_above60_btn。

android:nextFocusDown="@+id/bus_above60_btn" 

不會將焦點從「指揮編輯」文本字段更改爲該字段下面的按鈕。

我該如何做到這一點?

+1

我想'nextFocusDown'僅適用於EditText上,而不是按鈕。我想你將不得不在EditText –

回答

1

如果你只是想使按鈕集中在完成行動,首先你必須設置你的按鈕focusableInTouchMode作爲true

<Button 
    android:id="@+id/bus_above60_btn" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:text="Calculate" 
    android:focusableInTouchMode="true" 
    android:textSize="17sp" 
    android:textColor="#ffffff" 
    android:textStyle="bold" 
    android:background="#3498DB"/> 

現在,在您EditTextOnEditorActionListener,完成點擊後使按鈕聚焦:

bus_conductor_above60.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView textView, int action, KeyEvent keyEvent) { 
     if(action == EditorInfo.IME_ACTION_DONE) 
      bus_above60_btn.requestFocus(); 
     return false; 
    } 
}); 
+0

感謝的人使用'setOnEditorActionListener'。這工作! 但重點正在被橫幅廣告重疊。我能做些什麼來將重點放在橫幅廣告上方嗎? – Avid

+1

設置'android:focusableInTouchMode =「false」'橫幅廣告可以修復它 – SiSa

+0

不,它不工作! 按鈕正在向上滾動並集中在軟鍵盤上方,但在相同的位置,有橫幅廣告可阻止視圖 – Avid

1
<EditText 
      android:id="@+id/bus_conductor_above60" 
      android:imeOptions="actionDone" 
      /> 

在你的代碼,如果所有組件都在相同的佈局

bus_above60_btn.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         sameFunction(); 
        } 
       }); 

bus_conductor_above60 = findViewById(R.id.bus_conductor_above60); 

bus_conductor_above60 .setOnEditorActionListener(new TextView.OnEditorActionListener() { 
       @Override 
       public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
        if (actionId == EditorInfo.IME_ACTION_DONE) { 
         sameFunction(); 
        } 
        return false; 
       } 
      }); 
1

您的代碼將工作。因此,首先將您的按鈕放置在放置其他editText的相同佈局然後嘗試。

1

AFAIK有兩種方法讓你的目標

1.我的佈局工作完全按照你想要的:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:gravity="center" 
    android:orientation="vertical" 
    android:padding="15dp"> 

    <android.support.design.widget.TextInputLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <EditText 
      android:id="@+id/etUsername" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:maxLines="1" 
      android:hint="@string/username" 
      android:paddingLeft="10dp" 
      android:inputType="text" 
      android:paddingRight="10dp" 
      android:paddingStart="10dp" 
      android:paddingEnd="10dp" /> 

    </android.support.design.widget.TextInputLayout> 

    <android.support.design.widget.TextInputLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:passwordToggleEnabled="true" 
     > 

     <EditText 
      android:id="@+id/etPassword" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="20dp" 
      android:maxLines="1" 
      android:hint="@string/password" 
      android:inputType="textPassword" 
      android:paddingLeft="10dp" 
      android:paddingRight="10dp" 
      android:paddingStart="10dp" 
      android:paddingEnd="10dp" /> 
    </android.support.design.widget.TextInputLayout> 

    <Button 
     android:id="@+id/btnLogin" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="30dp" 
     android:background="@color/colorPrimary" 
     android:text="@string/login" 
     android:textColor="@color/white" 
     android:textSize="17sp" /> 
</LinearLayout> 

它的原因,from documentation,如下:

焦點運動基於一種算法,該算法在給定方向上查找最近的鄰居。在極少數情況下,默認算法 可能與開發人員的預期行爲不匹配。在這些 情況下,您可以通過使用這些XML佈局文件 屬性提供明確的覆蓋[...]

所以,如果你把按鈕,在edittexts的相同的佈局,它應該管理的未來自動對焦。

2.管理 「下一個」 點擊editText

Taken from this SO post

edittext.setOnKeyListener(new View.OnKeyListener() { 
     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { 
       Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show(); 
       return true; 
      } 
      return false; 
     } 
    }); 

希望這有助於!

相關問題