在活动-android中使用滚动视图停止编辑文本获得焦点

在活动-android中使用滚动视图停止编辑文本获得焦点,android,android-edittext,scrollview,Android,Android Edittext,Scrollview,关于如何阻止edittext专注于活动启动,有许多问题和答案 但是,所有的解决方案都对我有用,并且edittext在启动时不会获得焦点,但这会阻止滚动视图滚动 我在帖子中尝试了几乎所有的解决方案: 而且它不适用于活动中的滚动视图 这给我带来了很多麻烦。任何帮助都会很好 谢谢 尝试了以下操作: 用于防止AutoCompleteTextView接收焦点的虚拟项 设置编辑文本的属性:设置属性android:focusable=“true”和android:focusableInTouchMode=“t

关于如何阻止edittext专注于活动启动,有许多问题和答案

但是,所有的解决方案都对我有用,并且edittext在启动时不会获得焦点,但这会阻止滚动视图滚动

我在帖子中尝试了几乎所有的解决方案: 而且它不适用于活动中的滚动视图

这给我带来了很多麻烦。任何帮助都会很好

谢谢

尝试了以下操作:

  • 用于防止AutoCompleteTextView接收焦点的虚拟项
  • 设置编辑文本的属性:设置属性android:focusable=“true”和android:focusableInTouchMode=“true”
  • 以下是我的xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/content_home"
    
    
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
    
        tools:showIn="@layout/activity_home">
    
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:fitsSystemWindows="false">
    
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
    
                <ProgressBar
                    android:id="@+id/progressBar"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_centerInParent="true"
                    android:visibility="gone" />
    
    
                <TextView
                    android:id="@+id/textViewLinkToOrder"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="20dp"
                    android:layout_marginTop="20dp"
                    android:gravity="center"
                    android:text="Click here to book travel insurance."
                    android:textColor="@color/colorPrimary"
                    android:textSize="24sp"
    
                    />
    
                <LinearLayout
                    android:id="@+id/buttons_linear_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/textViewLinkToOrder"
                    android:orientation="horizontal">
    
                    <Button
                        android:id="@+id/choose_country_button"
                        android:layout_width="200dp"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/textViewLinkToOrder"
                        android:layout_centerHorizontal="true"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="5dp"
                        android:layout_weight="1"
                        android:background="@color/colorPrimary"
                        android:onClick="onChooseCountryButtonClick"
                        android:text="Choose country"
                        android:textColor="#ffff" />
    
    
                    <Button
                        android:id="@+id/automatic_country_button"
                        android:layout_width="200dp"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/choose_country_button"
                        android:layout_centerHorizontal="true"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="5dp"
                        android:layout_weight="1"
                        android:background="@color/colorPrimary"
                        android:text="My Location"
                        android:textColor="#fff" />
    
    
                </LinearLayout>
    
    
                <LinearLayout
                    android:id="@+id/linearLayoutAllDetails"
                    android:layout_width="match_parent"
    
                    android:layout_height="wrap_content"
                    android:layout_below="@id/buttons_linear_layout"
                    android:orientation="vertical">
    
                    <TextView
                        android:id="@+id/textView_coumtry_name"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:padding="5dp"
                        android:text="No country yet selected"
                        android:textColor="#000000"
                        android:textSize="30sp"
                        android:textStyle="bold" />
    
                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#e8e8e8"
                        android:padding="10dp">
    
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal">
    
    
                            <LinearLayout
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:orientation="horizontal">
    
                                <EditText
                                    android:id="@+id/editTextFromCurrency"
                                    android:layout_width="80sp"
                                    android:layout_height="50sp"
    
                                    android:background="@drawable/edit_txt_bg"
                                    android:ems="10"
                                    android:inputType="number"
                                    android:text="1" />
    
                                <TextView
                                    android:id="@+id/textViewFromCurrency"
                                    android:layout_width="wrap_content"
                                    android:layout_height="match_parent"
                                    android:layout_marginLeft="10dp"
                                    android:layout_marginStart="10dp"
                                    android:gravity="center"
                                    android:text="None"
                                    android:textColor="#000000"
                                    android:textSize="17sp" />
    
                            </LinearLayout>
    
                            <LinearLayout
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:orientation="horizontal">
    
                                <EditText
                                    android:id="@+id/editTextToCurrency"
                                    android:layout_width="80sp"
                                    android:layout_height="50sp"
                                    android:background="@drawable/edit_txt_bg"
    
                                    android:ems="10"
                                    android:inputType="number"
                                    android:text="1" />
    
                                <TextView
                                    android:id="@+id/textViewToCurrency"
                                    android:layout_width="wrap_content"
                                    android:layout_height="match_parent"
                                    android:layout_marginLeft="10dp"
                                    android:layout_marginStart="10dp"
                                    android:gravity="center"
                                    android:text="New Israeli Shekel"
                                    android:textColor="#000000"
                                    android:textSize="17sp" />
                            </LinearLayout>
                        </LinearLayout>
                    </RelativeLayout>
    
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal"
                        android:paddingBottom="20dp"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp"
                        android:paddingTop="20dp">
    
                        <ImageView
                            android:layout_width="40dp"
                            android:layout_height="40dp"
                            android:src="@drawable/police" />
    
                        <TextView
                            android:id="@+id/textView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginLeft="10dp"
                            android:layout_marginRight="10dp"
                            android:text="Police"
                            android:textColor="#000000"
                            android:textSize="17sp" />
    
                        <TextView
                            android:id="@+id/police_text_view"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginLeft="10dp"
                            android:layout_marginRight="10dp"
                            android:text=""
                            android:textColor="#000000"
                            android:textSize="17sp" />
    
                        <View
                            android:layout_width="0dp"
                            android:layout_height="0dp"
                            android:layout_weight="1" />
    
                        <ImageView
                            android:id="@+id/police_phone_button"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_gravity="center_vertical"
                            android:src="@android:drawable/sym_action_call" />
    
                    </LinearLayout>
    
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:paddingBottom="20dp"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp"
                        android:paddingTop="20dp">
    
                        <ImageView
                            android:layout_width="40dp"
                            android:layout_height="40dp"
                            android:src="@drawable/ambulance" />
    
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginLeft="10dp"
                            android:layout_marginRight="10dp"
                            android:text="Ambulance"
                            android:textColor="#000000"
                            android:textSize="17sp" />
    
                        <TextView
                            android:id="@+id/ambulance_text_view"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginLeft="10dp"
                            android:layout_marginRight="10dp"
                            android:text=""
                            android:textColor="#000000"
                            android:textSize="17sp" />
    
                        <View
                            android:layout_width="0dp"
                            android:layout_height="0dp"
                            android:layout_weight="1" />
    
                        <ImageView
                            android:id="@+id/ambulance_phone_button"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_gravity="center_vertical"
                            android:src="@android:drawable/sym_action_call" />
    
                    </LinearLayout>
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:paddingBottom="20dp"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp"
                        android:paddingTop="20dp">
    
                        <ImageView
                            android:layout_width="40dp"
                            android:layout_height="40dp"
                            android:src="@drawable/israel_consulate" />
    
                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:orientation="vertical">
    
                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center_vertical"
                                android:layout_marginLeft="10dp"
                                android:layout_marginRight="10dp"
                                android:text="Israel Consulate"
                                android:textColor="#000000"
                                android:textSize="17sp" />
    
                            <TextView
                                android:id="@+id/israel_consulate_text_view"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center_vertical"
                                android:layout_marginLeft="10dp"
                                android:layout_marginRight="10dp"
                                android:text=""
                                android:textColor="#000000"
                                android:textSize="17sp" />
                        </LinearLayout>
    
                        <View
                            android:layout_width="0dp"
                            android:layout_height="0dp"
                            android:layout_weight="1" />
    
                        <ImageView
                            android:id="@+id/israel_phone_button"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_gravity="center_vertical"
                            android:src="@android:drawable/sym_action_call"
    
                            />
                    </LinearLayout>
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:paddingBottom="20dp"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp"
                        android:paddingTop="20dp">
    
                        <ImageView
                            android:layout_width="40dp"
                            android:layout_height="40dp"
                            android:src="@drawable/chabad" />
    
    
                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:orientation="vertical">
    
                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center_vertical"
                                android:layout_marginLeft="10dp"
                                android:layout_marginRight="10dp"
                                android:text="Chabad"
                                android:textColor="#000000"
                                android:textSize="17sp" />
    
                            <TextView
                                android:id="@+id/chabad_number_text_view"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center_vertical"
                                android:layout_marginLeft="10dp"
                                android:layout_marginRight="10dp"
                                android:text=""
                                android:textColor="#000000"
                                android:textSize="17sp" />
    
    
                            <TextView
                                android:id="@+id/chabad_address_text_view"
                                android:layout_width="150dp"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center_vertical"
                                android:layout_marginLeft="10dp"
                                android:layout_marginRight="10dp"
                                android:text=""
                                android:textColor="#000000"
                                android:textSize="13sp" />
    
                        </LinearLayout>
    
                        <View
                            android:layout_width="0dp"
                            android:layout_height="0dp"
                            android:layout_weight="1" />
    
                        <ImageView
                            android:id="@+id/chabad_phone_button"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_gravity="center_vertical"
                            android:src="@android:drawable/sym_action_call" />
    
                    </LinearLayout>
                </LinearLayout>
            </RelativeLayout>
        </ScrollView>
    </RelativeLayout>
    

    获得所需结果的另一种方法是隐藏设备的虚拟键盘。这将禁止编辑文本获得焦点,因此不会滚动屏幕。您可以通过以下功能实现这一点:

    public static void hideKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        //Find the currently focused view, so we can grab the correct window token from it.
        View view = activity.getCurrentFocus();
        //If no view currently has focus, create a new one, just so we can grab a window token from it
        if (view == null) {
            view = new View(activity);
        }
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    
    这样调用:
    hideKeyboard(this),优先在onCreate()方法内部


    如果用户点击EditText,虚拟键盘将再次显示。

    共享您的布局和阻止scrollview滚动的代码您是否尝试过此操作?尝试将滚动视图嵌套在框架布局中。您是否尝试过使用此
    android:DegenantFocusability=“blocksDescendants”
    在root ScrollView中这不起作用这没有隐藏键盘可能是因为您有两个编辑文本。仅出于测试原因,尝试排除一个。这是防弹的,相信我。您可以实现一个函数,一旦使用了第一个EditText,就可以显示第二个(非常简单:第二个函数以xml attribs“hidden”开始,然后是“setVisibility”)。好的,这是一个简单明了的scrollview,具有linearlayout、nice、intelligent和objective的特性,适用于许多API。你不需要说谢谢,宝贝。要快乐。