gpt4 book ai didi

android - 即使在对话框窗口上使用 SOFT_INPUT_ADJUST_RESIZE,显示键盘时我的自定义 View DialogFragment 也不会调整大小

转载 作者:行者123 更新时间:2023-11-30 00:10:59 25 4
gpt4 key购买 nike

我有以下自定义 View DialogFragment

enter image description here

当我点击 EditText 时,会显示软键盘。目前,观察结果是

  1. 对话框的位置有点“上推”。
  2. 对话框未调整大小且对话框内容被阻止。

这是它的样子。

enter image description here

我不希望对话框内容被键盘覆盖。我已经根据https://stackoverflow.com/a/36295587/72437做了修改

getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

仍然,它根本没有帮助。

我也试过

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

没有区别。 Dialog 未调整大小并且 Dialog 内容被阻止。除了在第一次时会立即显示软键盘,而不需要用户立即点击 EditText

请问如何在显示软键盘时调整我的 Dialog 大小?


更新

我通过在 Activity 上应用相同的 XML 布局文件做了一个实验。 Activity 调整大小没有问题。

看起来 XML 布局文件本身没问题。

我使用的软输入方式是

<activity android:name="org.yccheok.jstock.gui.trading.sign_in.SignInFragmentActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize|stateAlwaysVisible" />

enter image description here


这是完整的代码和布局文件。

源代码

package org.yccheok.jstock.gui.trading.sign_in;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.Button;

import org.yccheok.jstock.gui.R;

/**
* Created by yccheok on 8/1/2018.
*/

public class SignInDialogFragment extends DialogFragment {

public static SignInDialogFragment newInstance() {
SignInDialogFragment signInDialogFragment = new SignInDialogFragment();
return signInDialogFragment;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Activity activity = getActivity();

// Get the layout inflater
LayoutInflater inflater = LayoutInflater.from(activity);

final View view = createView(this, inflater, null);

final AlertDialog.Builder builder = new AlertDialog.Builder(activity);

final AlertDialog dialog = builder.setView(view).create();

dialog.setCanceledOnTouchOutside(true);

final ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}

makeDialogShorter(dialog);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
});

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

return dialog;
}

private static View createView(final Fragment fragment, final LayoutInflater inflater, final ViewGroup container) {
View v = inflater.inflate(R.layout.trading_sign_in_fragment, container, false);

Button forgotPasswordButton = (Button)v.findViewById(R.id.forgot_password_button);
Button signInButton = (Button)v.findViewById(R.id.sign_in_button);

forgotPasswordButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});

signInButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
}
});

signInButton.setEnabled(false);

return v;
}

private static void makeDialogShorter(Dialog dialog) {
// http://stackoverflow.com/questions/19326142/why-listview-expand-collapse-animation-appears-much-slower-in-dialogfragment-tha
int width = dialog.getWindow().getDecorView().getWidth();

DisplayMetrics displayMetrics = new DisplayMetrics();
dialog.getOwnerActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;

height = Math.min(
(int)(height * 4.0 / 5.0),
dialog.getWindow().getDecorView().getHeight()
);

if (height > width) {
dialog.getWindow().setLayout(width, height);
}
}
}

布局 XML

<?xml version="1.0" encoding="utf-8"?>
<ViewAnimator xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/view_animator"
android:animateFirstView="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<RelativeLayout
android:id="@+id/sign_in_relative_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/trading_activity_vertical_margin"
android:paddingBottom="0dp"
android:layout_above="@+id/sign_in_bottom_nav_bar">

<android.support.design.widget.TextInputLayout
android:layout_marginLeft="@dimen/trading_activity_horizontal_margin"
android:layout_marginRight="@dimen/trading_activity_horizontal_margin"

app:hintTextAppearance="@style/TradingWizardTextInputLayout"
android:id="@+id/username_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/wizard_username"
android:id="@+id/username_edit_text"

android:inputType="textVisiblePassword|textNoSuggestions"
android:imeOptions="actionNext|flagNoExtractUi" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
android:layout_marginLeft="@dimen/trading_activity_horizontal_margin"
android:layout_marginRight="@dimen/trading_activity_horizontal_margin"

app:hintTextAppearance="@style/TradingWizardTextInputLayout"
app:passwordToggleEnabled="true"
android:id="@+id/password_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/wizard_password"
android:id="@+id/password_edit_text"

android:inputType="textPassword"
android:imeOptions="actionNext|flagNoExtractUi" />
</android.support.design.widget.TextInputLayout>

</LinearLayout>

<Button
style="?android:attr/buttonBarButtonStyle"
android:id="@+id/forgot_password_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:enabled="true"
android:textAllCaps="false"
android:text="@string/forgot_password"
android:textSize="16sp"
android:layout_above="@+id/sign_in_bottom_nav_bar"
android:layout_centerHorizontal="true"

android:layout_marginBottom="8dp"
android:paddingLeft="32dp"
android:paddingRight="32dp" />

<LinearLayout
android:background="?attr/welcomeBottomNavBarBackground"
android:orientation="horizontal"
android:id="@+id/sign_in_bottom_nav_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
style="?android:attr/buttonBarButtonStyle"
android:background="?attr/selectableItemBackground"

android:id="@+id/sign_in_button"
android:layout_width="0dp"
android:width="0dp"
android:layout_weight="1.0"
android:layout_height="48dp"
android:gravity="center"
android:layout_gravity="center"
android:enabled="true"
android:textAllCaps="true"
android:text="@string/log_in" />
</LinearLayout>
</RelativeLayout>
</ViewAnimator>

最佳答案

经过调试,发现是

  1. 不需要调用设置对话框高度的makeDialogShorter
  2. 不需要在 onGlobalLayout 期间调整软输入。

这是我的解决方案。我重构代码以使用 onCreateView 而不是 onCreateDialog,以使代码更短更简单。

package org.yccheok.jstock.gui.trading.sign_in;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;

import org.yccheok.jstock.gui.R;

/**
* Created by yccheok on 8/1/2018.
*/

public class SignInDialogFragment extends DialogFragment {

public static SignInDialogFragment newInstance() {
SignInDialogFragment signInDialogFragment = new SignInDialogFragment();
return signInDialogFragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.trading_sign_in_fragment, container);

Button forgotPasswordButton = (Button)view.findViewById(R.id.forgot_password_button);
Button signInButton = (Button)view.findViewById(R.id.sign_in_button);

forgotPasswordButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});

signInButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
}
});

signInButton.setEnabled(false);


//set to adjust screen height automatically, when soft keyboard appears on screen
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

return view;
}
}

这是结果

enter image description here

我们现在有一个简短可爱的小对话:)

关于android - 即使在对话框窗口上使用 SOFT_INPUT_ADJUST_RESIZE,显示键盘时我的自定义 View DialogFragment 也不会调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48144185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com