java - 警报对话框的按钮被切断

标签 java android android-alertdialog

我有一个警告对话框,它会在有人触摸特定按钮时启动。警报对话框包含一个由布局 xml 文件定义的 View 。此布局包含几个控件,例如复选框、编辑文本字段……这些控件都在 ScrollView 中,以便您可以滚动浏览内容,如果屏幕上放不下整个内容。

现在的问题是,当我启动这个警告对话框时,底部的按钮被截断了。尽管滚动条可以正常工作并且我可以滚动浏览警报对话框的内容,但警报对话框的按钮有时并不完全可见。

这意味着: 有时,一切都很好,我可以看到警报对话框的按钮,有时由于奇怪的原因,按钮被切断了。我认为这是一个问题,因为 View 对于警报对话框来说太大了,并且将按钮向下推得更多。 例如, View 包含一个编辑文本控件。如果我在那里输入我的名字,一切都很好。但是如果我在这个编辑文本中添加一个新行,按钮就会开始被切断一点。 我做错了什么?我认为 ScrollView 会处理我的 View 过大,以便警报对话框适合屏幕。 我的应用始终处于纵向模式。

View 代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

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

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/bitteeinstellungenwaehlen" />

        <EditText
            android:id="@+id/edtTeam1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/hteam1"
            android:text="Christoph" >

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/edtTeam2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/hteam2"
            android:text="Lea" />

        <EditText
            android:id="@+id/edtTeam3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/hteam3"
            android:text="Ludwig" />

        <EditText
            android:id="@+id/edtTeam4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/hteam4"
            android:text="Anja" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/weitereeinstellungen" />

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

            <CheckBox
                android:id="@+id/chkModerationMode"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/moderationsmodus" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="@string/info" />

        </LinearLayout>

        <CheckBox
            android:id="@+id/chkPassOver"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/weitergeben" />

        <CheckBox
            android:id="@+id/chkBlackScreen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/blankscreen" />

    </LinearLayout>

</ScrollView>

这就是我启动警报对话框的方式:

    final AlertDialog.Builder alert = new AlertDialog.Builder(QuizEditor.this);

    alert.setTitle(getString(R.string.speichernUndVorschau) + "...");
    alert.setMessage("");


    LayoutInflater inflater = LayoutInflater.from(QuizEditor.this);
    final View layFrage = inflater.inflate(R.layout.layoutquizsettings,null);
    alert.setView(layFrage);

    final CheckBox chkModeration = (CheckBox) layFrage.findViewById(R.id.chkModerationMode);
    final CheckBox chkPassOver = (CheckBox) layFrage.findViewById(R.id.chkPassOver);
    final CheckBox chkBlackScreen = (CheckBox) layFrage.findViewById(R.id.chkBlackScreen);

    final EditText edtTeam1 = (EditText) layFrage.findViewById(R.id.edtTeam1);
    final EditText edtTeam2 = (EditText) layFrage.findViewById(R.id.edtTeam2);
    final EditText edtTeam3 = (EditText) layFrage.findViewById(R.id.edtTeam3);
    final EditText edtTeam4 = (EditText) layFrage.findViewById(R.id.edtTeam4);



    alert.setNeutralButton(getString(R.string.speichernUndVorschau), new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {




    }
    });

    alert.setNegativeButton(getString(R.string.abbrechen), new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
        // Canceled.
      }
    });



    final AlertDialog dialog2 = alert.create();

    dialog2.setOnShowListener(new OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {
            // TODO Auto-generated method stub
            Button btnStarten = (Button) dialog2.getButton(AlertDialog.BUTTON_NEUTRAL);
            btnStarten.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    ArrayList<String> teams = new ArrayList<String>();

                    String team1 = edtTeam1.getText().toString().trim();
                    String team2 = edtTeam2.getText().toString().trim();
                    String team3 = edtTeam3.getText().toString().trim();
                    String team4 = edtTeam4.getText().toString().trim();

                    if(team1.length() > 0) teams.add(team1);
                    if(team2.length() > 0) teams.add(team2);
                    if(team3.length() > 0) teams.add(team3);
                    if(team4.length() > 0) teams.add(team4);

                    if(teams.size() == 0) {
                        Toast.makeText(QuizEditor.this, getString(R.string.keinteameingegeben), Toast.LENGTH_SHORT).show();

                    }
                    else
                    {
                        // Quiz starten



                        dialog2.dismiss();
                        Intent myIntent;

                        if(chkBlackScreen.isChecked()) {
                            myIntent = new Intent(QuizEditor.this, BlackScreen.class);
                        }
                        else // Direkt das Quiz starten
                        {
                            myIntent = new Intent(QuizEditor.this, Quiz.class);
                        }


                        myIntent.putStringArrayListExtra("teams", teams);
                        myIntent.putExtra("moderation", chkModeration.isChecked());
                        myIntent.putExtra("passover", chkPassOver.isChecked());
                        myIntent.putExtra("filename", filename);


                        QuizEditor.this.startActivity(myIntent);
                    }
                }

            });

        }

    });

    dialog2.show();
    // dialog2.getWindow().setLayout(LayoutParams.WRAP_CONTENT, 300);

我对德语单词感到抱歉。在图像中,您可以看到问题所在。 不幸的是,我不允许上传我制作的屏幕截图......

最佳答案

这个问题已经在评论中解决了,但我会添加一个完整的答案,并表明它对我也有用。

对于带有自定义 View 的 AlertDialog,不要使用 .setMessage 行。在我的项目中删除以下行导致按钮停止被剪裁。

.setMessage("This is my message")

关于java - 警报对话框的按钮被切断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14003243/

相关文章:

java - JScrollPane 没有按预期工作,为什么?

java - 这是java能做而c不能做的事情吗?

java - Android:将数据备份到互联网的最佳方式?

更新后 Android gradle 3.0.0 buildConfigField 警告

c# - 按后退按钮时显示警报

java - 如何修复 ic_launcher_foregorund.xml 意外标记和未找到顶级元素

java - 如何在 JSP 中使用 JavaBeans 显示对象数组

android - 如何在 WizarDroid 中跳过 WizardStep

android - 在半透明 Activity 中显示警报对话框

Intent 返回 Activity 后,android alertdialog 不显示