Xamarin での watchOS テキスト入力の操作

Apple Watch では、ユーザーがテキストを入力するためのキーボードは提供されていませんが、次のウォッチに適した代替手段がいくつかサポートされています。

  • 定義済みのテキスト オプションの一覧から選択
  • Siri ディクテーション
  • 絵文字の選択
  • 手書き文字認識スクリブル (watchOS 3 で導入)

シミュレーターでは現在ディクテーションがサポートされていませんが、次に示すように、テキスト入力コントローラーの他のオプション (スクリブルなど) をテストすることはできます。

Testing the scribble option

ウォッチ アプリでテキスト入力を受け入れるには:

  1. 定義済みのオプションの文字列配列を作成します。
  2. 配列、絵文字を許可するかどうか、ユーザーの入力完了時に呼び出される Action を指定して、PresentTextInputController を呼び出します。
  3. 完了アクションで、入力結果をテストし、アプリで適切なアクション (ラベルのテキスト値を設定するなど) を実行します。

次のコード スニペットは、3 つの事前定義されたオプションをユーザーに示します。

var suggest = new string[] {"Get groceries", "Buy gas", "Post letter"};

PresentTextInputController (suggest, WatchKit.WKTextInputMode.AllowEmoji, (result) => {
    // action when the "text input" is complete
    if (result != null && result.Count > 0) {
    // this only works if result is a text response (Plain or AllowEmoji)
        enteredText = result.GetItem<NSObject>(0).ToString();
        Console.WriteLine (enteredText);
        // do something, such as myLabel.SetText(enteredText);
    }
});

WKTextInputMode 列挙型には次の 3 つの値があります。

  • プレーン
  • AllowEmoji
  • AllowAnimatedEmoji

プレーン

プレーン モードが設定されている場合、ユーザーは次を選択できます。

  • ディクテーション
  • スクリブル
  • アプリケーションが提供する事前定義されたリストから

Dictation, Scribble, or from a pre-defined list that the app supplies

結果は常に、string にキャストできる NSObject として返されます。

Emoji

絵文字には次の 2 種類があります。

  • 通常の Unicode 絵文字
  • アニメーション画像

ユーザーが Unicode 絵文字を選択すると、それが文字列として返されます。

アニメーション画像の絵文字が選択されている場合、完了ハンドラーの result には、絵文字 UIImage を含む NSData オブジェクトが含まれます。

ディクテーションのみを受け入れる

提案 (またはスクリブル オプション) を表示せずに、ユーザーをディクテーション画面に直接誘導するには:

  • 候補リストに空の配列を渡し、
  • WatchKit.WKTextInputMode.Plain を設定します。
PresentTextInputController (new string[0], WatchKit.WKTextInputMode.Plain, (result) => {
    // action when the "text input" is complete
    if (result != null && result.Count > 0) {
        dictatedText = result.GetItem<NSObject>(0).ToString();
        Console.WriteLine (dictatedText);
        // do something, such as myLabel.SetText(dictatedText);
    }
});

ユーザーが話しているとき、ウォッチ画面には、理解されたテキスト ("This is a test" など) を含む次の画面が表示されます。

When the user is speaking, the watch screen displays the text as it is understood

[完了] ボタンを押すと、そのテキストが返されます。