5

AlertDialog とカスタム コンテンツ/ビューで問題に直面しています。簡単に言うと、ソフトキーボードが開いているときに AlertDialog のサイズは変更されません。次のスクリーンショットは、私の問題と達成したいことを最もよく示しています。

現在の動作 (左) と必要な動作 (右)

現在の動作 望まれる行動

SOで同様の問題を抱えている他のスレッドがいくつかあることを私は知っています。残念ながら、提供されたソリューションはどれもうまくいきませんでした。私のサンプルコードに従ってください:

XML

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

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

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </EditText>
    </LinearLayout>

</ScrollView>

Java - CustomAlertDialog.class

public class CustomAlertDialog extends AlertDialog{

    private Context context;

    public CustomAlertDialog(Context context) {
        super(context);
        this.context = context;
    }

    public void buildDialog(){
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.dialog_content, null);
        builder = new AlertDialog.Builder(context);
        builder.setTitle("EditText");
        builder.setView(layout);
        builder.setPositiveButton("Ok", new OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                dismiss();
            }
        });
        builder.create();
    }

    public void showDialog(){
        builder.show();
    }
}

上記のクラス/関数は、私の Main.java クラスでボタンを押すと呼び出されます

btnAlertDialog.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            CustomAlertDialog dialog = new CustomAlertDialog(Main.this);
            dialog.buildDialog();
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            dialog.showDialog();
        }
    });

私は次のことを試しました:

このように LinearLayout にスクロールバーを追加する

android:scrollbars="vertical" 
android:scrollbarAlwaysDrawVerticalTrack="true"

結果:何も変わらない

ダイアログのフラグの設定:

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

結果:何も変わらない

カスタム スタイルを AlertDialog に適用する:

<style name="AlertDialog" parent="@android:style/Theme.Holo.Light">
        <item name="android:windowFullscreen">false</item>
</style>

ジャワ

builder = new AlertDialog.Builder(context, R.style.AlertDialog);

これはうまくいきました。残念ながら、次のスクリーンショットで確認できるいくつかの問題が発生しました

カスタム スタイル - 新しい問題

この問題に何日も苦労してきたので、助けていただければ幸いです。ありがとう!

解決

現在、次のメソッドを使用して Dialog を作成していCustomAlertDialog.classます。以前に機能しなかった理由の詳細については、 nandeeshの回答の下にある私のコメントを参照してください。

public void startDialog(){
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.dialog_content, null);
        builder = new AlertDialog.Builder(context);
        builder.setTitle("EditText");
        builder.setView(layout);
        builder.setPositiveButton("Ok", new OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                dismiss();
            }
        });
        AlertDialog aDialog = builder.create();
        aDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        aDialog.show();
    }
4

2 に答える 2

9

setSoftInputmodeをどこで実行したかわかりませんが、代わりに

builder.show();

使用する

AlertDialog mDialog = builder.create();
mDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
mDialog.show();

これが機能しない場合は、CustomAlertDialogクラスを投稿してください

于 2013-02-20T14:31:29.707 に答える
0

明らかに、この問題はダイアログの構築方法とは関係ありません。カスタム ビューなどを使用する場合、問題は windowSoftInputMode です。

ウィンドウの変更に加えて、アクティビティの AndroidManifest.xml で windowSoftInputMode を変更すると、うまくいくはずです。

ダイアログ内のビューが ScrollView であることを確認してください。

于 2013-02-19T14:13:03.323 に答える