我有一个相对简单的问题。我有一个包含很多EditText的活动。当我打开活动时,它会自动聚焦到第一个EditText并显示虚拟键盘。
我该如何防止这种情况?
答案 0 :(得分:203)
在XML文件的布局标记中使用此属性:
android:focusable="true"
android:focusableInTouchMode="true"
正如其他成员在评论中所报告的那样,它不适用于ScrollView
,因此您需要将这些属性添加到ScrollView
的主要子项中。
答案 1 :(得分:95)
您可以将此添加到Android Manifest活动中:
android:windowSoftInputMode="stateHidden|adjustResize"
答案 2 :(得分:30)
我在此处描述了几个实现,但现在我已将[{1}}添加到我的AndroidManifest.xml
属性中:
Activity
android:windowSoftInputMode="stateAlwaysHidden"
,我认为这也很简单。“ stateAlwaysHidden ”软键盘始终隐藏在 活动的主窗口有输入焦点。
答案 3 :(得分:11)
如果您对自己的活动有另一种观点,例如ListView
,您也可以这样做:
ListView.requestFocus();
在你的onResume()中从editText
获取焦点。
我知道这个问题已得到解答,但只是提供了一个对我有用的替代解决方案:)
答案 4 :(得分:5)
https://stackoverflow.com/a/11627976/5217837这几乎是正确的:
SELECT count(id) AS count,
sum((SELECT COALESCE(sum(amount), 0.00)
FROM purchases
WHERE purchases.user_id = users.id
AND payment_status = "Completed"
)) AS purchases_amount,
sum((SELECT COALESCE(sum(round(amount) * 10), 0)
FROM purchases
WHERE users.id = purchases.user_id
AND payment_status = "Completed"
)) AS coins_earned,
sum((SELECT count(purchase_id)
FROM purchases
WHERE users.id = purchases.user_id
AND payment_status = "Completed"
)) AS purchases_count
FROM (`users`)
WHERE `referred_by` = '1'
AND `created` >= '2015-01-01 00:00:00' // created = user's registration date
AND `created` <= '2015-12-31 23:59:59'
但它应该是SOFT_INPUT_STATE_HIDDEN而不是SOFT_INPUT_STATE_ALWAYS_VISIBLE
答案 5 :(得分:3)
我有一个类似的问题,即使在平板电脑上使用Android 3.2.1切换标签时,键盘会自动弹出并保持不动。使用以下方法:
public void setEditTextFocus(EditText searchEditText, boolean isFocused)
{
searchEditText.setCursorVisible(isFocused);
searchEditText.setFocusable(isFocused);
searchEditText.setFocusableInTouchMode(isFocused);
if (isFocused) {
searchEditText.requestFocus();
} else {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(searchEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS );
}
}
在onCreate()和每个EditText的活动的onPause()中:
setEditTextFocus(myEditText,false);
对于每个EditText和OnTouchListener:
myEditText.setOnTouchListener(new EditText.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
setEditTextFocus(myEditText, true);
return false;
}
});
EditText
中的每个OnEditorActionListener
:
myEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
.......
setEditTextFocus(myEditText, false);
return false;
}
});
对于EditText
中的每个layout xml
:
android:imeOptions="actionDone"
android:inputType="numberDecimal|numberSigned" // Or something else
可能有更多代码优化。
答案 6 :(得分:3)
在您的活动代码中使用它:
@Override
public void onCreate(Bundle savedInstanceState) {
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
答案 7 :(得分:3)
((InputMethodManager)getActivity().getSystemService("input_method")).hideSoftInputFromWindow(this.edittxt.getWindowToken(), 0);
答案 8 :(得分:2)
我找到了这个适合我的简单解决方案。在父布局中设置这些属性:
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.38.0 (20140413.2041)
-->
<!-- Title: graphname Pages: 1 -->
<svg width="708pt" height="305pt" viewBox="0.00 0.00 707.95 305.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 301)">
<title>graphname</title>
<polygon fill="white" stroke="none" points="-4,4 -4,-301 703.946,-301 703.946,4 -4,4" />
<!-- 0 -->
<g id="node1" class="node">
<title>0</title>
<ellipse fill="#b2dfee" stroke="#b2dfee" cx="456.946" cy="-192" rx="40.0939" ry="18" />
<text text-anchor="middle" x="456.946" y="-188.3" font-family="Times New Roman,serif" font-size="14.00">Mumbai</text>
</g>
<!-- 1 -->
<g id="node2" class="node">
<title>1</title>
<ellipse fill="#b2dfee" stroke="#b2dfee" cx="399.946" cy="-18" rx="46.5926" ry="18" />
<text text-anchor="middle" x="399.946" y="-14.3" font-family="Times New Roman,serif" font-size="14.00">Bangalore</text>
</g>
<!-- 0->1 -->
<g id="edge1" class="edge">
<title>0->1</title>
<path fill="none" stroke="grey" d="M460.066,-163.558C462.067,-134.55 461.467,-88.3313 442.946,-54 438.559,-45.8673 431.415,-38.9123 424.2,-33.3953" />
<polygon fill="grey" stroke="grey" points="456.557,-163.514 459.204,-173.773 463.532,-164.103 456.557,-163.514" />
<text text-anchor="middle" x="509.946" y="-101.3" font-family="Times New Roman,serif" font-size="14.00"> BLR-MUM-440 </text>
</g>
<!-- 6 -->
<g id="node7" class="node">
<title>6</title>
<ellipse fill="#b2dfee" stroke="#b2dfee" cx="339.946" cy="-105" rx="27" ry="18" />
<text text-anchor="middle" x="339.946" y="-101.3" font-family="Times New Roman,serif" font-size="14.00">Goa</text>
</g>
<!-- 0->6 -->
<g id="edge6" class="edge">
<title>0->6</title>
<path fill="none" stroke="grey" d="M406.617,-188.92C382.78,-185.106 356.24,-176.165 340.946,-156 333.916,-146.73 333.971,-133.465 335.621,-122.928" />
<polygon fill="grey" stroke="grey" points="406.251,-192.402 416.627,-190.258 407.179,-185.464 406.251,-192.402" />
<text text-anchor="middle" x="391.946" y="-144.8" font-family="Times New Roman,serif" font-size="14.00"> GOA-MUM-108 </text>
</g>
<!-- 2 -->
<g id="node3" class="node">
<title>2</title>
<ellipse fill="#b2dfee" stroke="#b2dfee" cx="118.946" cy="-105" rx="37.0935" ry="18" />
<text text-anchor="middle" x="118.946" y="-101.3" font-family="Times New Roman,serif" font-size="14.00">Cochin</text>
</g>
<!-- 3 -->
<g id="node4" class="node">
<title>3</title>
<ellipse fill="#b2dfee" stroke="#b2dfee" cx="118.946" cy="-192" rx="32.4942" ry="18" />
<text text-anchor="middle" x="118.946" y="-188.3" font-family="Times New Roman,serif" font-size="14.00">Jaipur</text>
</g>
<!-- 3->2 -->
<g id="edge8" class="edge">
<title>3->2</title>
<path fill="none" stroke="grey" d="M118.946,-163.734C118.946,-150.419 118.946,-134.806 118.946,-123.175" />
<polygon fill="grey" stroke="grey" points="115.446,-163.799 118.946,-173.799 122.446,-163.799 115.446,-163.799" />
<text text-anchor="middle" x="162.446" y="-144.8" font-family="Times New Roman,serif" font-size="14.00"> COC-JAI-983 </text>
</g>
<!-- 3->6 -->
<g id="edge5" class="edge">
<title>3->6</title>
<path fill="none" stroke="grey" d="M154.314,-176.995C170.385,-170.615 189.636,-162.951 206.946,-156 223.413,-149.388 227.386,-147.373 243.946,-141 268.615,-131.506 297.089,-121.207 316.581,-114.256" />
<polygon fill="grey" stroke="grey" points="152.768,-173.843 144.763,-180.784 155.349,-180.35 152.768,-173.843" />
<text text-anchor="middle" x="287.946" y="-144.8" font-family="Times New Roman,serif" font-size="14.00"> GOA-JAI-884 </text>
</g>
<!-- 4 -->
<g id="node5" class="node">
<title>4</title>
<ellipse fill="#b2dfee" stroke="#b2dfee" cx="40.9464" cy="-279" rx="40.8928" ry="18" />
<text text-anchor="middle" x="40.9464" y="-275.3" font-family="Times New Roman,serif" font-size="14.00">Kolkatta</text>
</g>
<!-- 4->1 -->
<g id="edge3" class="edge">
<title>4->1</title>
<path fill="none" stroke="grey" d="M29.5176,-251.287C19.1406,-222.958 7.48741,-177.368 21.9464,-141 34.1426,-110.324 44.1274,-103.101 72.9464,-87 163.431,-36.4467 286.981,-23.3374 353.307,-20.029" />
<polygon fill="grey" stroke="grey" points="26.3613,-252.829 33.2323,-260.894 32.8903,-250.305 26.3613,-252.829" />
<text text-anchor="middle" x="68.4464" y="-144.8" font-family="Times New Roman,serif" font-size="14.00"> BLR-KOL-228 </text>
</g>
<!-- 4->3 -->
<g id="edge7" class="edge">
<title>4->3</title>
<path fill="none" stroke="grey" d="M62.7013,-254.293C76.1886,-239.595 93.1216,-221.142 104.866,-208.344" />
<polygon fill="grey" stroke="grey" points="59.8,-252.278 55.6176,-262.012 64.9576,-257.011 59.8,-252.278" />
<text text-anchor="middle" x="127.446" y="-231.8" font-family="Times New Roman,serif" font-size="14.00"> JAI-KOL-743 </text>
</g>
<!-- 5 -->
<g id="node6" class="node">
<title>5</title>
<ellipse fill="#b2dfee" stroke="#b2dfee" cx="522.946" cy="-279" rx="29.4969" ry="18" />
<text text-anchor="middle" x="522.946" y="-275.3" font-family="Times New Roman,serif" font-size="14.00">Delhi</text>
</g>
<!-- 5->0 -->
<g id="edge4" class="edge">
<title>5->0</title>
<path fill="none" stroke="grey" d="M492.042,-261.717C484.599,-256.574 477.27,-250.295 471.946,-243 464.895,-233.337 461.127,-220.345 459.132,-210.027" />
<polygon fill="grey" stroke="grey" points="490.4,-264.82 500.718,-267.226 494.152,-258.911 490.4,-264.82" />
<text text-anchor="middle" x="520.446" y="-231.8" font-family="Times New Roman,serif" font-size="14.00"> MUM-DEL-340 </text>
</g>
<!-- 5->1 -->
<g id="edge9" class="edge">
<title>5->1</title>
<path fill="none" stroke="grey" d="M553.607,-261.01C559.788,-256.059 565.453,-250.047 568.946,-243 587.287,-205.999 727.222,-322.519 562.946,-87 535.932,-48.2704 482.434,-31.5297 444.09,-24.3379" />
<polygon fill="grey" stroke="grey" points="551.149,-258.463 545.015,-267.101 555.197,-264.173 551.149,-258.463" />
<text text-anchor="middle" x="654.446" y="-144.8" font-family="Times New Roman,serif" font-size="14.00"> BLR-DEL-270 </text>
</g>
<!-- 6->1 -->
<g id="edge2" class="edge">
<title>6->1</title>
<path fill="none" stroke="grey" d="M340.326,-76.4424C341.465,-68.7133 343.716,-60.6211 347.946,-54 353.629,-45.1046 362.471,-37.9253 371.263,-32.4387" />
<polygon fill="grey" stroke="grey" points="336.824,-76.3034 339.381,-86.5849 343.794,-76.9527 336.824,-76.3034" />
<text text-anchor="middle" x="395.446" y="-57.8" font-family="Times New Roman,serif" font-size="14.00"> BLR-GOA-432 </text>
</g>
</g>
</svg>
现在,当活动开始时,此主要布局将默认获得焦点。
此外,我们可以通过再次将焦点放在主布局上来从运行时的子视图中移除焦点,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
希望它对你有用。
答案 9 :(得分:1)
这是我正在使用的解决方案,不是最好的解决方案,但它对我来说效果很好
editComment.setFocusableInTouchMode(false);
editComment.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event)
{
// TODO Auto-generated method stub
editComment.setFocusableInTouchMode(true);
editComment.requestFocus() ;
return false;
}});
答案 10 :(得分:1)
有趣的是,此文档https://developer.android.com/training/keyboard-input/visibility.html指出,当活动开始并将焦点放在文本字段时,软键盘不会显示(然后继续显示如何在需要时显示键盘用一些代码)。
在我的三星Galaxy S5上,这是我的应用程序(没有清单条目或特定代码)的工作原理 - 没有软键盘。然而,在Lollipop AVD上,显示了一个软键盘 - 违反了上面给出的文档。
如果在AVD中进行测试时遇到此问题,您可能希望在真实设备上进行测试,看看会发生什么。
答案 11 :(得分:1)
以下帖子提供了一些很好的答案:Stop EditText from gaining focus at Activity startup。我经常使用的是Morgan下面的代码:
<!-- 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"/>
注意:虚拟物品必须在可聚焦元素之前放置。
而且我认为即使使用ScrollView它也应该可以正常工作,并且对此无法获得任何可访问性问题。
答案 12 :(得分:0)
在您的活动代码中使用它:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
答案 13 :(得分:0)
search_edit_text =(EditText)findViewById(R.id.search_edit_text);
search_edit_text.requestFocus();
search_edit_text.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
search_edit_text.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
这对我来说很有效片段可能具有一些不同的语法。 这是为了活动
答案 14 :(得分:0)
android:focusableInTouchMode="true"
将以上行添加到具有焦点并导致EditText
弹出的TextInputLayout
或softInputKeyboard
的xml中。
这为我们解决了问题,现在键盘没有弹出
答案 15 :(得分:0)
将以下代码添加到活动XML的顶部,并确保“视图”位于EditText上方
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:focusableInTouchMode="true"/>
答案 16 :(得分:0)
机器人:windowSoftInputMode = “stateHidden | adjustResize”
工作正常
答案 17 :(得分:0)
接受的答案对我不起作用,这就是为什么给出答案工作的解决方案,可能会有所帮助!
EditText edt = (EditText) findViewById(R.id.edt);
edt.requestFocus();
edt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
edt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
现在键盘打开享受:)
答案 18 :(得分:0)
当您的EditText在活动开始时自动获得焦点时会发生这种情况。因此,一种简单而稳定的解决方法就是将初始焦点设置为任何其他视图,例如Button等。
您可以在布局XML中执行此操作,无需代码。
答案 19 :(得分:-1)
如果您的视图包含EditText和Listview,则默认情况下将打开键盘。 要隐藏键盘默认弹出,请执行以下操作
this.listView.requestFocus();
确保在获取editText视图后请求关注listview。
例如
this.listView = (ListView) this.findViewById(R.id.list);
this.editTextSearch = (EditText) this.findViewById(R.id.editTextSearch);
this.listView.requestFocus();
如果你这样做,那么editText将获得焦点并弹出键盘。