【问题标题】:soft keyboard doesn't close and freeze when close软键盘关闭时不关闭并冻结
【发布时间】:2015-02-23 04:17:31
【问题描述】:

场景

我的应用程序是从 Activity C (A->B->C) 打开的第 3 方应用程序。之后,第 3 方应用程序完成它返回到我在 Activity C 上的应用程序。一切正常,直到 Activity C 中只有一个 EditText 正在使用软键盘输入并且我想关闭它。

软键盘冻结而不关闭,我的应用也没有冻结。

  • 我的测试设备基于 Android KitKat。
  • 我更改为默认和自定义的其他键盘.. 没有希望
  • 我按下主页按钮并再次返回我的应用程序软键盘行为正常。

这是我的示例 XML(重现此错误)

活动 A

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Go to Activity B"
            android:id="@+id/btnNext"/>
</LinearLayout>

活动 B

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Go To Activity C"
            android:id="@+id/btnNext"/>
</LinearLayout>

活动 C

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbarStyle="outsideOverlay">



            <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/padding_medium"
                    android:background="#DEE1E3"
                    android:id="@+id/llReceiverContainer">


                <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                    <EditText
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_margin="@dimen/padding_default"
                            android:hint="Receiver Name"
                            android:id="@+id/edtReceiver"/>
                </LinearLayout>

                <Button
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/button_height_big"
                        android:layout_marginBottom="@dimen/padding_default"
                        android:layout_marginLeft="@dimen/padding_default"
                        android:layout_marginRight="@dimen/padding_default"
                        android:text="Ex. Call 3rd Party App"
                        android:textColor="#fff"
                        android:textStyle="bold"
                        android:textSize="@dimen/font_large"
                        android:id="@+id/btnTestMpos"/>

            </LinearLayout>

</ScrollView>

这是我的示例类(A->B->C 只需调用 startActivity)

类活动C

public class ActivityC extends Activity {


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

        Button btnTestMpos = (Button) findViewById(R.id.btnTestMpos);
        EditText edtUser = (EditText) findViewById(R.id.edtReceiver);
        btnTestMpos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                payWithMPOS();
            }
        });
    }

    private void payWithMPOS() {
        //call thire pparty application
    }

    // return from 3rd party application
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        getResultFromMPOS();
    }

    public void getResultFromMPOS() {
        // extract result from 3rd party applicaiton
    }
}

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.ui.keyboard"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="14"/>
    <uses-permission android:name="android.permission.GET_TASKS" >
    </uses-permission>


    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <activity android:name=".ActivityA"
                  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=".ActivityB"
                  android:label="@string/app_name">
        </activity>
        <activity android:name=".ActivityC"
                  android:launchMode="singleTask"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="com.example.mpos.integration" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
</manifest>

【问题讨论】:

    标签: android


    【解决方案1】:

    在你的 Activity C Java 类中试试这个

    ScrollView yourScrollViewName= (Button) findViewById(R.id.yourScrollViewId);
    yourScrollViewName.setOnTouchListener(this);
    
    @Override
        public boolean onTouch(View v, MotionEvent event) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
            return false;
    
        }
    

    【讨论】:

    • javalangnull 先生,下面我试试
    • 当用户在编辑文本之外触摸或切换到 Activity C 时,是否要隐藏软键盘?
    • 我想在edittext上键入后关闭键盘,但它没有关闭并且软键盘变成了冻结
    • 好的,您是否为您的编辑文本设置了特定的字符限制??
    • 不..?键盘不关闭和冻结有必要吗??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 2023-03-07
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多