Java 键盘出现在编辑文本字段&;这样就把它藏起来了。如何防止键盘隐藏EditText字段?

Java 键盘出现在编辑文本字段&;这样就把它藏起来了。如何防止键盘隐藏EditText字段?,java,android,xml,android-layout,Java,Android,Xml,Android Layout,我的EditText位于屏幕底部,所以当我点击它进行编辑时,键盘会隐藏它。我希望当我点击EditText时,键盘应该保持在它的下方&不应该隐藏它 以下是点击前显示编辑文本的屏幕截图链接: 以下是点击EditText后屏幕截图的链接(EditText字段现在位于键盘下方): 以下是我的SettingUpUserProfile.java文件的代码: public class SettingUpUserProfile extends AppCompatActivity { public st

我的EditText位于屏幕底部,所以当我点击它进行编辑时,键盘会隐藏它。我希望当我点击EditText时,键盘应该保持在它的下方&不应该隐藏它

以下是点击前显示编辑文本的屏幕截图链接:

以下是点击EditText后屏幕截图的链接(EditText字段现在位于键盘下方):

以下是我的SettingUpUserProfile.java文件的代码:

public class SettingUpUserProfile extends AppCompatActivity {

    public static final int TAKE_PHOTO_REQUEST = 0;
    public static final int PICK_PHOTO_REQUEST = 1;
    private static final int RESULT_LOAD_IMG = 2;
    String imgDecodableString;
    protected ImageView userProfilePicture;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_up_user_profile);

        userProfilePicture = (ImageView) findViewById(R.id.userProfilePicture);
        userProfilePicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SettingUpUserProfile.this);
                builder.setTitle(null);
                builder.setItems(R.array.pickImage_options, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int position) {
                        switch (position) {
                            case 0:
                                Intent intentCaptureFromCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                startActivityForResult(intentCaptureFromCamera, TAKE_PHOTO_REQUEST);
                                break;
                            case 1:
                                Intent chooseFromGalley = new Intent(Intent.ACTION_GET_CONTENT);
                                chooseFromGalley.setType("image/*");
                                startActivityForResult(chooseFromGalley, PICK_PHOTO_REQUEST);
                                break;
                        }
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            // When an Image is picked
            Uri uri = data.getData();

            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                // Log.d(TAG, String.valueOf(bitmap));

                ImageView imageView = (ImageView) findViewById(R.id.userProfilePicture);
                imageView.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                    .show();
        }

    }
}
<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:background="@color/light_purple"
    tools:context="com.abc.xyz.SettingUpUserProfile">

    <TextView
        android:id="@+id/settingUpUserProfileText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="@string/settingUpUserProfileText1"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:textSize="30sp"
        android:gravity="center_horizontal|center_vertical"/>

    <ImageView
        android:id="@+id/userProfilePicture"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@+id/settingUpUserProfileText1"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="100dp"
        android:clickable="true"
        android:src="@drawable/ic_face_white_48dp" />

    <TextView
        android:id="@+id/settingUpUserProfileText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/userProfilePicture"
        android:layout_marginTop="5dp"
        android:text="@string/settingUpUserProfileText2"
        android:textColor="@color/white"
        android:textSize="15sp"
        android:gravity="center_horizontal|center_vertical"/>

    <EditText
        android:id="@+id/userName"
        android:background="@drawable/phone_number_edit_text_design"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/settingUpUserProfileText2"
        android:layout_marginTop="80dp"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginEnd="10dp"
        android:gravity="center_horizontal|center_vertical"
        android:hint="@string/hint_userName"
        android:textColor="@color/white"
        android:textColorHint="#E0E0E0"
        android:textCursorDrawable="@null"
        android:inputType="textPersonName"/>

    <Button
        android:id="@+id/buttonAllSet"
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/userName"
        android:layout_marginTop="20dp"
        android:text="@string/button_allSet"
        android:textStyle="bold"
        android:textColor="@color/light_purple"
        android:layout_marginEnd="120dp"
        android:layout_marginStart="120dp"
        android:gravity="center_horizontal|center_vertical"/>

</RelativeLayout>
以下是我的活动\u设置\u用户\u profile.xml文件的代码

public class SettingUpUserProfile extends AppCompatActivity {

    public static final int TAKE_PHOTO_REQUEST = 0;
    public static final int PICK_PHOTO_REQUEST = 1;
    private static final int RESULT_LOAD_IMG = 2;
    String imgDecodableString;
    protected ImageView userProfilePicture;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_up_user_profile);

        userProfilePicture = (ImageView) findViewById(R.id.userProfilePicture);
        userProfilePicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SettingUpUserProfile.this);
                builder.setTitle(null);
                builder.setItems(R.array.pickImage_options, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int position) {
                        switch (position) {
                            case 0:
                                Intent intentCaptureFromCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                startActivityForResult(intentCaptureFromCamera, TAKE_PHOTO_REQUEST);
                                break;
                            case 1:
                                Intent chooseFromGalley = new Intent(Intent.ACTION_GET_CONTENT);
                                chooseFromGalley.setType("image/*");
                                startActivityForResult(chooseFromGalley, PICK_PHOTO_REQUEST);
                                break;
                        }
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            // When an Image is picked
            Uri uri = data.getData();

            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                // Log.d(TAG, String.valueOf(bitmap));

                ImageView imageView = (ImageView) findViewById(R.id.userProfilePicture);
                imageView.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                    .show();
        }

    }
}
<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:background="@color/light_purple"
    tools:context="com.abc.xyz.SettingUpUserProfile">

    <TextView
        android:id="@+id/settingUpUserProfileText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="@string/settingUpUserProfileText1"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:textSize="30sp"
        android:gravity="center_horizontal|center_vertical"/>

    <ImageView
        android:id="@+id/userProfilePicture"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@+id/settingUpUserProfileText1"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="100dp"
        android:clickable="true"
        android:src="@drawable/ic_face_white_48dp" />

    <TextView
        android:id="@+id/settingUpUserProfileText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/userProfilePicture"
        android:layout_marginTop="5dp"
        android:text="@string/settingUpUserProfileText2"
        android:textColor="@color/white"
        android:textSize="15sp"
        android:gravity="center_horizontal|center_vertical"/>

    <EditText
        android:id="@+id/userName"
        android:background="@drawable/phone_number_edit_text_design"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/settingUpUserProfileText2"
        android:layout_marginTop="80dp"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginEnd="10dp"
        android:gravity="center_horizontal|center_vertical"
        android:hint="@string/hint_userName"
        android:textColor="@color/white"
        android:textColorHint="#E0E0E0"
        android:textCursorDrawable="@null"
        android:inputType="textPersonName"/>

    <Button
        android:id="@+id/buttonAllSet"
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/userName"
        android:layout_marginTop="20dp"
        android:text="@string/button_allSet"
        android:textStyle="bold"
        android:textColor="@color/light_purple"
        android:layout_marginEnd="120dp"
        android:layout_marginStart="120dp"
        android:gravity="center_horizontal|center_vertical"/>

</RelativeLayout>

我真的不知道在这里该做什么

请让我知道

我是StackOverflow的新手,请配合


提前感谢。

选项-1

尝试在AndroidManifest.xml中的活动中使用android:WindowsOfInputMode=“adjustPan”

选项2

在xml中使用
ScrollView
作为父级。将xml替换为以下内容:

<ScrollView 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"
    tools:context="com.abc.xyz.SettingUpUserProfile" >

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/light_purple">

    <TextView
        android:id="@+id/settingUpUserProfileText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="@string/settingUpUserProfileText1"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:textSize="30sp"
        android:gravity="center_horizontal|center_vertical"/>

    <ImageView
        android:id="@+id/userProfilePicture"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@+id/settingUpUserProfileText1"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="100dp"
        android:clickable="true"
        android:src="@drawable/ic_face_white_48dp" />

    <TextView
        android:id="@+id/settingUpUserProfileText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/userProfilePicture"
        android:layout_marginTop="5dp"
        android:text="@string/settingUpUserProfileText2"
        android:textColor="@color/white"
        android:textSize="15sp"
        android:gravity="center_horizontal|center_vertical"/>

    <EditText
        android:id="@+id/userName"
        android:background="@drawable/phone_number_edit_text_design"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/settingUpUserProfileText2"
        android:layout_marginTop="80dp"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginEnd="10dp"
        android:gravity="center_horizontal|center_vertical"
        android:hint="@string/hint_userName"
        android:textColor="@color/white"
        android:textColorHint="#E0E0E0"
        android:textCursorDrawable="@null"
        android:inputType="textPersonName"/>

    <Button
        android:id="@+id/buttonAllSet"
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/userName"
        android:layout_marginTop="20dp"
        android:text="@string/button_allSet"
        android:textStyle="bold"
        android:textColor="@color/light_purple"
        android:layout_marginEnd="120dp"
        android:layout_marginStart="120dp"
        android:gravity="center_horizontal|center_vertical"/>

</RelativeLayout>

</ScrollView>

我遇到了同样的问题,软键盘位于屏幕底部的编辑文本视图的顶部。我能够通过在我的AndroidManifest.xml文件的相关活动中添加一行来找到解决方案。 将布局放在滚动视图中

android:WindowsOfInputMode=“调整大小|状态隐藏” 这是整个活动标记的外观:

<activity
        android:name="com.my.MainActivity"
        android:screenOrientation="portrait"
        android:label="@string/title_activity_main"
        android:windowSoftInputMode="adjustResize|stateHidden" >
    </activity>


这里最重要的值是adjustResize。这将使整个UI向上移动,为软键盘留出空间。

只需在xml文件中使用
滚动视图作为父视图。


    <?xml version="1.0" encoding="utf-8"?>

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.abc.xyz.SettingUpUserProfile" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/light_purple" >

            <TextView
                android:id="@+id/settingUpUserProfileText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="40dp"
                android:gravity="center_horizontal|center_vertical"
                android:text="@string/settingUpUserProfileText1"
                android:textColor="@color/white"
                android:textSize="30sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/userProfilePicture"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_below="@+id/settingUpUserProfileText1"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_marginTop="100dp"
                android:clickable="true"
                android:src="@drawable/ic_face_white_48dp" />

            <TextView
                android:id="@+id/settingUpUserProfileText2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/userProfilePicture"
                android:layout_marginTop="5dp"
                android:gravity="center_horizontal|center_vertical"
                android:text="@string/settingUpUserProfileText2"
                android:textColor="@color/white"
                android:textSize="15sp" />

            <EditText
                android:id="@+id/userName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/settingUpUserProfileText2"
                android:layout_marginEnd="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginStart="10dp"
                android:layout_marginTop="80dp"
                android:background="@drawable/phone_number_edit_text_design"
                android:gravity="center_horizontal|center_vertical"
                android:hint="@string/hint_userName"
                android:inputType="textPersonName"
                android:textColor="@color/white"
                android:textColorHint="#E0E0E0"
                android:textCursorDrawable="@null" />

            <Button
                android:id="@+id/buttonAllSet"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/userName"
                android:layout_marginEnd="120dp"
                android:layout_marginStart="120dp"
                android:layout_marginTop="20dp"
                android:background="@color/white"
                android:gravity="center_horizontal|center_vertical"
                android:text="@string/button_allSet"
                android:textColor="@color/light_purple"
                android:textStyle="bold" />
        </RelativeLayout>
    </LinearLayout>
</ScrollView>

谢谢大家的回答

我通过将我的activity\u setting\u up\u user\u profile.xml文件的代码更改为以下代码来解决此问题:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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:orientation="vertical"
    android:background="@color/light_purple"
    tools:context="com.abc.xyz.SettingUpUserProfile" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TextView
                android:id="@+id/settingUpUserProfileText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="40dp"
                android:gravity="center_horizontal|center_vertical"
                android:text="@string/settingUpUserProfileText1"
                android:textColor="@color/white"
                android:textSize="30sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/userProfilePicture"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_below="@+id/settingUpUserProfileText1"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_marginTop="100dp"
                android:clickable="true"
                android:src="@drawable/ic_face_white_48dp" />

            <TextView
                android:id="@+id/settingUpUserProfileText2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/userProfilePicture"
                android:layout_marginTop="5dp"
                android:gravity="center_horizontal|center_vertical"
                android:text="@string/settingUpUserProfileText2"
                android:textColor="@color/white"
                android:textSize="15sp" />

            <EditText
                android:id="@+id/userName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/settingUpUserProfileText2"
                android:layout_marginEnd="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginStart="10dp"
                android:layout_marginTop="80dp"
                android:background="@drawable/phone_number_edit_text_design"
                android:gravity="center_horizontal|center_vertical"
                android:hint="@string/hint_userName"
                android:inputType="textPersonName"
                android:textColor="@color/white"
                android:textColorHint="#E0E0E0"
                android:textCursorDrawable="@null" />

            <Button
                android:id="@+id/buttonAllSet"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/userName"
                android:layout_marginEnd="120dp"
                android:layout_marginStart="120dp"
                android:layout_marginTop="20dp"
                android:background="@color/white"
                android:gravity="center_horizontal|center_vertical"
                android:text="@string/button_allSet"
                android:textColor="@color/light_purple"
                android:textStyle="bold" />

        </RelativeLayout>
    </ScrollView>
    </LinearLayout>


这太酷了

只需在xml文件中使用ScrollView作为父视图\代码是否有任何错误或只是不起作用?我感谢您的努力,兄弟,但它仍然没有发生!好的,最后一件事,活动模式是横向的吗?不,是纵向的。现在的问题是,我第一次点击EditText时,它正在向上滚动。但在按下后退按钮并再次点击EditText后,它不会向上滚动!这在某种程度上是可行的,但编辑文本几乎在点击它4-5分钟后就会向上滚动。这确实可行,但是底部视图现在已经被破坏了。