LoginSignup
5

More than 5 years have passed since last update.

iPhoneのテキストフィールド外をタップしてキーボードを閉じる方法(Titanium Mobile)

Last updated at Posted at 2014-05-15

カジュアルにキーボード隠したいー!
http://qiita.com/yuch_i/items/65baa9586afdfc6aeb34
これをTitanium Mobileでやりたいよー!!

やった。

方針

  • キーボードを閉じるのは、テキストフィールドオブジェクトのblur()を呼ぶといいみたい。なので、今どのテキストフィールドのキーボードが出てるのかを保存する。

  • Windowにクリックイベントを仕込んで、キーボードを閉じる。

  • テキストフィールドをクリックしたときにWindowのクリックイベントへ伝播しないように、e.cancelBubble=true;で伝播を止める。xmlのほうにbubbleParent=false と書いてもいいけど、これだとどんなイベントもとめちゃうのであまりおすすめしない。

できたやつ

test.js
var args = arguments[0] || {};
var currentKeyboardOwner = undefined;

function SetKeyboardOwner(e) {
    currentKeyboardOwner = e.source;
    e.cancelBubble=true;
}

function HideKeyboard(e) {
    if(typeof(currentKeyboardOwner) !== 'undefined') {
        currentKeyboardOwner.blur();
    }
}
test.xml
<Alloy>
    <NavigationWindow id="navigationWindow" platform="ios">
        <Window class="container" title="First Win" onClick="HideKeyboard">
            <TextField id="textField1" borderStyle="Ti.UI.INPUT_BORDERSTYLE_ROUNDED" color="#336699" top="10" left="10" width="250" height="60"  onClick="SetKeyboardOwner" />
            <TextField id="textField2" borderStyle="Ti.UI.INPUT_BORDERSTYLE_ROUNDED" color="#336699" top="100" left="10" width="250" height="60" onClick="SetKeyboardOwner" />
        </Window>
    </NavigationWindow>
</Alloy>

いまのところ良い感じに動いている。

ださいところ

  • 全部のテキストフィールドにonClick="SetKeyboardOwner"って書いてるとこ。これなんかフォーカス中のオブジェクトがグローバルパラメタに入ってたりしないのかな。ちょっと調べたけど分からず。教えてください!!

おしまい

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5