type_null - ontouchevent edittext android
Activityの起動時にEditTextがフォーカスを得ないようにする (20)
LucとMarkの優れた回答ですが、優れたコードサンプルはありません。 次の例のように<LinearLayout>
android:focusableInTouchMode="true"
<LinearLayout>
android:focusableInTouchMode="true"
タグを追加すると問題が解決します。
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:nextFocusUp="@id/autotext"
android:nextFocusLeft="@id/autotext"/>
私はAndroidで2つの要素を持つActivity
を持っています。
-
EditText
-
ListView
私のActivity
が開始されると、 EditText
すぐに入力フォーカス(カーソルの点滅)になります。 起動時にコントロールに入力フォーカスを設定したくありません。 私は試した:
EditText.setSelected(false);
運がありません。 Activity
開始時にEditText
が自分自身を選択しないようにするにはどうすればよいですか。
ListView
ようにあなたの活動について別の見方がある場合は、次のようにすることもできます。
ListView.requestFocus();
あなたのonResume()
からフォーカスを取得します。
私はこの質問が答えられたことを知っていますが、私のために働いた代替ソリューションを提供するだけです:)
layout
最初のTextView
で"focusable"と"focusable in touch mode"をtrueに設定するだけTextView
。 このようにして、アクティビティの開始時にTextView
がフォーカスされますが、その性質上、画面にフォーカスされているものは何も表示されず 、もちろんキーボードも表示されません 。
onCreate
メソッドに以下を追加します。
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
うん私も同じことをした - 最初の焦点を取得する「ダミー」の線形レイアウトを作成します。 さらに、「次の」フォーカスIDを設定して、ユーザーが一度スクロールした後にそれ以上フォーカスできなくなるようにします。
<LinearLayout 'dummy'>
<EditText et>
dummy.setNextFocusDownId(et.getId());
dummy.setNextFocusUpId(et.getId());
et.setNextFocusUpId(et.getId());
ビューへのフォーカスを取り除くための多くの作業
ありがとう
この行を親レイアウトに書きます...
android:focusableInTouchMode="true"
これは完璧で最も簡単な解決策です。私はいつもこれを私のアプリで使用しています。
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
これを実現するには、レイアウトの幅と高さを0dp
に設定したダミーのEditText
を作成し、そのビューにフォーカスを要求します。 XMLレイアウトに次のコードスニペットを追加します。
<EditText
android:id="@+id/editText0"
android:layout_width="0dp"
android:layout_height="0dp"
android:hint="@string/dummy"
android:ems="10"
>
<requestFocus />
</EditText>
アクティビティの作成時に、EditText要素にuse clearFocus()
を追加するだけです。 例えば、
edittext = (EditText) findViewById(R.id.edittext);
edittext.clearFocus();
また、フォーカスを別の要素に変更したい場合は、 requestFocus()
を使用してください。 例えば、
button = (Button) findViewById(R.id.button);
button.requestFocus();
キーボードを開けたくないActivity
Manifest
ファイル内にこのコードを書いてください。
android:windowSoftInputMode="stateHidden"
マニフェストファイル:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.projectt"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="24" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
**android:windowSoftInputMode="stateHidden"**
android:label="@string/app_name" >
</activity>
</application>
</manifest>
以下はManifest
で私のために働きました。 書きます 、
<activity
android:name=".MyActivity"
android:windowSoftInputMode="stateAlwaysHidden"/>
問題は、私がレイアウトのXML form
でしか見ることができない特性から来ているようです。
EditText
XMLタグ内の宣言の最後でこの行を必ず削除してください。
<requestFocus />
それはそのような何かを与えるはずです:
<EditText
android:id="@+id/emailField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress">
//<requestFocus /> /* <-- without this line */
</EditText>
最も単純な答えは、これをXMLの親レイアウトに追加するだけです。
android:focusable="true"
android:focusableInTouchMode="true"
最初の編集可能フィールドの前にこれを試してください。
<TextView
android:id="@+id/dummyfocus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/foo"
/>
----
findViewById(R.id.dummyfocus).setFocusableInTouchMode(true);
findViewById(R.id.dummyfocus).requestFocus();
次はedittextが作成されたときにフォーカスを合わせるのを止めますが、触ったときにそれをつかみます。
<EditText
android:id="@+id/et_bonus_custom"
android:focusable="false" />
そのため、xmlでfocusableをfalseに設定しましたが、キーはjavaにあり、これに次のリスナーを追加します。
etBonus.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.setFocusable(true);
v.setFocusableInTouchMode(true);
return false;
}
});
あなたはfalseを返している、すなわちイベントを消費していないので、フォーカシングの振る舞いは通常通り進行します。
私がした最も簡単なことはonCreateで別のビューに焦点を合わせることです:
myView.setFocusableInTouchMode(true);
myView.requestFocus();
これによりソフトキーボードの起動が停止し、EditTextにカーソルが点滅しなくなりました。
私にとって、すべてのデバイスでうまくいったのはこれです。
<!-- fake first focusable view, to allow stealing the focus to itself when clearing the focus from others -->
<View
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
これを問題のある集中的なビューの前にビューとして配置するだけです。
私はいくつかの答えを個別に試してみましたが、焦点はまだEditTextにあります。 私は、以下の解決策のうちの2つを一緒に使ってそれを解決することができただけです。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
(Silver https://.com/a/8639921/15695からの参照)
と削除
<requestFocus />
EditTextで
(floydaddict shareからの参照)
簡単な解決策: AndroidManifest
でActivity
タグを使用する
android:windowSoftInputMode="stateAlwaysHidden"
遅くなりましたが、おそらく役に立ちました。 レイアウトの一番上にダミーのEditTextを作成してから、 onCreate()
でmyDummyEditText.requestFocus()
を呼び出します。
<EditText android:id="@+id/dummyEditTextFocus"
android:layout_width="0px"
android:layout_height="0px" />
それは私の期待通りに振る舞うようです。 設定の変更などを処理する必要はありません。長いTextView(手順)を使用してActivityにこれを必要としました。