Hou*_*fly 81 android android-edittext
在我的应用程序中,我所有屏幕的第一个视图是EditText,因此每次进入屏幕时,屏幕键盘都会弹出.如何禁用此弹出窗口并在手动单击EditText时启用它?
eT = (EditText) findViewById(R.id.searchAutoCompleteTextView_feed);
eT.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(eT.getWindowToken(), 0);
}
}
});
Run Code Online (Sandbox Code Playgroud)
xml代码:
<ImageView
android:id="@+id/feedPageLogo"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/wic_logo_small" />
<Button
android:id="@+id/goButton_feed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/go" />
<EditText
android:id="@+id/searchAutoCompleteTextView_feed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/goButton_feed"
android:layout_toRightOf="@id/feedPageLogo"
android:hint="@string/search" />
<TextView
android:id="@+id/feedLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/feedPageLogo"
android:gravity="center_vertical|center_horizontal"
android:text="@string/feed"
android:textColor="@color/white" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ButtonsLayout_feed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:id="@+id/feedButton_feed"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_margin="0dp"
android:layout_weight="1"
android:background="@color/white"
android:text="@string/feed"
android:textColor="@color/black" />
<Button
android:id="@+id/iWantButton_feed"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_margin="0dp"
android:layout_weight="1"
android:background="@color/white"
android:text="@string/iwant"
android:textColor="@color/black" />
<Button
android:id="@+id/shareButton_feed"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_margin="0dp"
android:layout_weight="1"
android:background="@color/white"
android:text="@string/share"
android:textColor="@color/black" />
<Button
android:id="@+id/profileButton_feed"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_margin="0dp"
android:layout_weight="1"
android:background="@color/white"
android:text="@string/profile"
android:textColor="@color/black" />
</LinearLayout>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/feedListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/ButtonsLayout_feed"
android:layout_below="@id/feedLabel"
android:textSize="15dp" >
</ListView>
Run Code Online (Sandbox Code Playgroud)
第三个视图(EditText)是焦点所在的位置.
Sky*_*net 167
最佳解决方案位于Project Manifest文件(AndroidManifest.xml)中,在activity构造中添加以下属性
机器人:windowSoftInputMode = "stateHidden"
例:
<activity android:name=".MainActivity"
android:windowSoftInputMode="stateHidden" />
Run Code Online (Sandbox Code Playgroud)
描述:
介绍于:
注意: 此处设置的值("stateUnspecified"和"adjustUnspecified"除外)将覆盖主题中设置的值.
Hou*_*fly 81
您必须在EditText上方创建一个视图,该视图采用"假"焦点:
就像是 :
<!-- Stop auto focussing the EditText -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/transparent"
android:focusable="true"
android:focusableInTouchMode="true">
</LinearLayout>
<EditText
android:id="@+id/searchAutoCompleteTextView_feed"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:inputType="text" />
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我使用LinearLayout来请求焦点.希望这可以帮助.
这完美地工作......感谢Zaggo0
Mat*_*556 37
edittext.setShowSoftInputOnFocus(false);
Run Code Online (Sandbox Code Playgroud)
现在您可以使用您喜欢的任何自定义键盘.
A.B*_*.B. 22
人们在这里提出了许多很好的解决方案,但我在EditText中使用了这个简单的技术(java中没有任何东西,并且需要AnroidManifest.xml).只需在EditText上直接将focusable和focusableInTouchMode设置为false即可.
<EditText
android:id="@+id/text_pin"
android:layout_width="136dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textAlignment="center"
android:inputType="numberPassword"
android:password="true"
android:textSize="24dp"
android:focusable="false"
android:focusableInTouchMode="false"/>
Run Code Online (Sandbox Code Playgroud)
我的意图是在App锁定活动中使用此编辑框,我要求用户输入PIN码,我想显示我的自定义密码键盘.在Android Studio 2.1上使用minSdk = 8和maxSdk = 23进行测试
Son*_*ery 20
在您的活动类上添加以下代码.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Run Code Online (Sandbox Code Playgroud)
当用户单击EditText时,将弹出键盘
您可以使用以下代码禁用OnScreen键盘.
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(editText.getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)
两个简单的方案:
第一个解决方案添加在清单xml文件中的代码行下面.在Manifest文件(AndroidManifest.xml)中,在activity构造中添加以下属性
机器人:windowSoftInputMode = "stateHidden"
例:
<activity android:name=".MainActivity"
android:windowSoftInputMode="stateHidden" />
Run Code Online (Sandbox Code Playgroud)
第二个解决方案是在活动中添加以下代码行
//Block auto opening keyboard
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Run Code Online (Sandbox Code Playgroud)
我们可以使用上述任何一种解决方案.谢谢
为InputMethodManager声明全局变量:
private InputMethodManager im ;
Run Code Online (Sandbox Code Playgroud)
在onCreate()下定义它:
im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(youredittext.getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)
将onClickListener设置为oncreate()内的编辑文本:
youredittext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
im.showSoftInput(youredittext, InputMethodManager.SHOW_IMPLICIT);
}
});
Run Code Online (Sandbox Code Playgroud)
这会奏效.
| 归档时间: |
|
| 查看次数: |
137386 次 |
| 最近记录: |