当软键盘隐藏时,从EditText中隐藏光标。

6
这是我的第一个应用程序,我正在尝试找到一种方法,在按下返回按钮并且软键盘消失时从编辑框中移除光标。我尝试检查是否按下了返回按钮,但这只适用于在键盘打开的情况下按两次返回按钮。
以下是我的示例代码,包括一个editText和返回按钮检查:
public class MainActivity extends Activity implements OnItemSelectedListener, OnGlobalLayoutListener
{
    boolean flag;
    double vc, vs, t, r, c;
    EditText resistor_E_T;

@Override
public void onBackPressed()
{
    resistor_E_T.setCursorVisible( false );
}


@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    resistor_E_T = ( EditText ) findViewById( R.id.resistor_Edit_Text );
    resistor_E_T.addTextChangedListener( new TextWatcher( )
    {
        public void afterTextChanged( Editable s ) 
        {
            resistor_E_T.setCursorVisible( true );
            if ( Exceptions.isDouble( resistor_E_T.getText( ).toString( ) ) )
            {
                r = Double.parseDouble( resistor_E_T.getText( ).toString( ) );
            }
        }

        public void beforeTextChanged( CharSequence s, int start, int count, int after ) { }

        public void onTextChanged( CharSequence s, int start, int before, int count ) { }
   });

另外,我在互联网和stackoverflow上找到了一些杂项代码,可以通过像素检查键盘是否是向上的,但我无法使任何代码工作。有人有什么想法吗?我应该如何做或者它是不可能的。

现在发生了什么: http://i.stack.imgur.com/6alGK.png

当软键盘消失时,光标仍在闪烁: http://i.stack.imgur.com/wpbkl.png


你找到解决方案了吗? - the beest
1个回答

0

使用这个:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

或者您可以在xml中使用此代码:

android:windowSoftInputMode="stateHidden"

请告诉我它是否有效 :)


我在<EditText/>标签的xml中添加了android:windowSoftInputMode="stateHidden",但没有任何变化。我将其放在<RelativeLayout/>标签上,但仍然没有变化。最后,我在MainActivity类的编辑文本中插入了此代码“getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);”,但未解决问题,并且还禁用了返回按钮的功能(如果软键盘关闭,则可以通过一次按下将应用程序移至后台)。 - 2 X
在 XML 的父布局上添加 android:windowSoftInputMode="stateHidden",而不是在 EditText 上添加该属性...... - Dinesh R Rajput
或者将此代码放入onCreate方法中:"getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);" - Dinesh R Rajput
我在XML中插入了命令,但是没有任何反应。代码如下:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="top" android:windowSoftInputMode="stateHidden" 我也尝试了这个代码:this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 但还是没有任何反应。 - 2 X

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接