2016-03-02 56 views
1

在Appcelerator Titanium for iOS和Android中開發的應用程序中,當用戶單擊返回按鈕時,我想要在兩者之間導航的文本字段。保持鍵盤在文本字段之間焦點變化時打開

在Android上,這是通過設置返回鍵類型自動處理的。對於iOS,我必須添加事件偵聽器,當單擊返回按鈕時,它會觸發下一個字段的焦點。

但是,在焦點切換正在發生時,鍵盤正在動起來然後再次起來。代碼示例展示的行爲:

var win = Ti.UI.createWindow({width: Ti.UI.FILL,height: Ti.UI.FILL,backgroundColor:'white'}); 
var input1 = Ti.UI.createTextField({ 
    width: 50, height:20, top: 50, hintText: 'input1', 
    returnKeyType: Ti.UI.RETURNKEY_NEXT 
}); 
input1.addEventListener("return",function(){input2.focus();}); 
win.add(input1); 
var input2 = Ti.UI.createTextField({ 
    width: 50, height:20, top: 100, hintText: 'input2', 
    returnKeyType: Ti.UI.RETURNKEY_NEXT 
}); 
input2.addEventListener("return",function(){input1.focus();}); 
win.add(input2); 
win.open(); 

據那是以前給(後來刪除)一個答案,就可以保持鍵盤香草的iOS開放。因此 - 有沒有辦法使用這種技術來保持鍵盤在Appcelerator iOS中的文本框焦點切換過程中打開?謝謝!

回答

1

我花了幾個小時在這看着適用於iOS的鈦源之前(我真的不說話的OBJ。C)。最後,我瞭解到我所知道的一個財產與我預期的完全相反。所有你需要做的是設置suppressReturnfalse

我也給你提供我的便攜式代碼形式的前進到下一個字段上的回報:

<View id='signupForm' class='formContainer'> 
    <TextField id='emailField' name='email' class='loginField emailField' hintText='email' onReturn='selectNextField' onFocus='scrollToField' /> 
    <View class="hrLine"></View> 
    <TextField id='passwordField' name='password' class='loginField' hintText='password' passwordMask='true' onReturn='selectNextField' onFocus='scrollToField' /> 
    <View class="hrLine"></View> 
    <TextField id='fnameField' class='loginField' name='fname' hintText='first name' onReturn='selectNextField' onFocus='scrollToField' autocapitalization='Ti.UI.TEXT_AUTOCAPITALIZATION_WORDS'/> 
    <View class="hrLine"></View> 
    <TextField id='lnameField' class='loginField' name='lname' hintText='last name' returnKeyType='Ti.UI.RETURNKEY_GO' onReturn='submitSignup' onFocus='scrollToField' autocapitalization='Ti.UI.TEXT_AUTOCAPITALIZATION_WORDS'/> 
</View> 

樣式

'.loginField': { 
    height: 40, 
    width: '100%', 
    opacity: 1, 
    paddingLeft: 10, 
    returnKeyType: Ti.UI.RETURNKEY_NEXT, 
    // THE CRITICAL LINE 
    suppressReturn: false 
} 

控制器

// controller.js 
function selectNextField(e) { 
    var hint = e.source.getHintText(), 
     nextOne = false, 
     finished = false; 

    // find currently focused field then declare that the next field is THE ONE 
    _.each($.signupForm.getChildren(), function(view) { 
     if(finished) { 
      return; 
     } 

     if(view.getHintText && view.getHintText() === hint) { 
      nextOne = true; 
      return; 
     } 
     if(nextOne && view.getApiName() === 'Ti.UI.TextField') { 
      finished = true; 
      return view.focus(); 
     } 
    }); 
} 
+0

一條線解決了問題,愛它!對不起,延遲標記答案是正確的,設法錯過了你的答案。謝謝! – PlutonB

1

可你只需要使用returnKeyType :Titanium.UI.RETURNKEY_NEXT屬性的文本字段

鐵道部詳情Titanium.UI.TextField

+0

我在textfields上設置了這個屬性。在Android上,這已經足夠,但是不能在必須添加eventlistener的ios上進行,這會導致所描述的行爲。 – PlutonB

+0

代碼請... ...! –

+0

當然!編輯了OP並添加了一個完整的代碼示例,顯示iOS上的行爲。當我在模擬器中嘗試此操作時,鍵盤保持大約四分之一的時間,其他時候鍵盤會來回移動。 – PlutonB

相關問題