android 无法从xml文件显示预期的对话框

1bqhqjot  于 2023-02-27  发布在  Android
关注(0)|答案(1)|浏览(118)

image of xml layoutimage of output on emulator我试图写一个代码,使用xml布局文件点击菜单选项显示自定义对话框。在Android Studio中运行应用程序时,我没有得到所需的对话框,即EditText字段没有显示,布局也不同于我在xml文件中设置的,请帮助
这是onOptionsItemSelected块中的代码

//Add New Sample 
else if (id == R.id.new_sample) { 
final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); 
View view = getLayoutInflater().inflate(R.layout.dialogtoaddsample,null); 
EditText sampleNameinput = (EditText) view.findViewById(R.id.samplename_input); EditText genderinput = (EditText) view.findViewById(R.id.gender_input); 
Button submit = (Button) view.findViewById(R.id.submit_button); 
Button cancel = (Button) view.findViewById(R.id.cancel_button);    

alert.setView(view);

    final AlertDialog alertDialog = alert.create();
    alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    alertDialog.setContentView(R.layout.dialogtoaddsample);
    alertDialog.setCanceledOnTouchOutside(false);
    alertDialog.show();

    cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            alertDialog.dismiss();
        }
    });
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) { String samplenameEt = sampleNameinput.getText().toString().trim();
            String readpath = Contact.getFolderPath(MainActivity.this);
            String sameTemp = new String();
            File f = new File(readpath);
            if (f.exists()) {
                File file\[\] = f.listFiles();
                for (int i = 0; i \< file.length; i++) {
                    if (Objects.equals(file\[i\].getName(), samplenameEt)) {
                        sameTemp = samplenameEt;
                    }
                }
            }
            String currentDate = getDate();
            if (TextUtils.isEmpty(samplenameEt)) {
                Toast.makeText(MainActivity.this, "The sample name cannot be empty!",
                        Toast.LENGTH_SHORT).show();
            } else if (Objects.equals(sameTemp, samplenameEt)) {
                Toast.makeText(MainActivity.this, "The sample name cannot be same as others!",
                        Toast.LENGTH_SHORT).show();
            } else {
                //\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
                File foldersample = new File(Contact.getFolderPath(MainActivity.this) + File.separator + samplenameEt);
                boolean success = true;
                if (!foldersample.exists()) {
                    success = foldersample.mkdirs();
                }
                if (success) {
                    Toast.makeText(MainActivity.this, "Blood sample " + samplenameEt +
                            " is added!", Toast.LENGTH_SHORT).show();
                    db.addSamples(new SampleNameList());
                    showRecord();
                } else {
                    Toast.makeText(MainActivity.this, "Unable to create blood sample bank folder!"
                            , Toast.LENGTH_SHORT).show();
                }
            }

        }

    });

我已经附上了模拟器输出的图像和我的xml布局
这是XML代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_gravity="center"
    android:layout_width="300dp"
    android:layout_height="250dp">

    <RelativeLayout
        android:id="@+id/layout_dialog"
        android:layout_width="300dp"
        android:layout_height="250dp"/>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="53dp"
        android:gravity="center"
        android:textStyle="bold"
        android:text="Add New Blood Sample"
        android:textColor="@color/white"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/textView13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="33dp"
        android:layout_marginTop="57dp"
        android:text="Please enter the Name and Gender"
        android:textColor="@color/white" />

    <EditText
        android:id="@+id/gender_input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="40dp"
        android:layout_marginBottom="74dp"
        android:hint="Gender"
        android:ems="10"
        android:gravity="start|top"

        android:inputType="textMultiLine"
        android:textColor="@color/white" />

    <EditText
        android:id="@+id/samplename_input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="40dp"
        android:layout_marginBottom="124dp"
        android:hint="Name"
        android:ems="10"
        android:inputType="textMultiLine"
        android:textColor="@color/white" />

    <Button
        android:id="@+id/submit_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="28dp"
        android:layout_marginBottom="14dp"
        android:text="Submit" />

    <Button
        android:id="@+id/cancel_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="28dp"
        android:layout_marginBottom="14dp"
        android:text="Cancel" />

</RelativeLayout>
oyxsuwqo

oyxsuwqo1#

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="300dp"
    android:layout_height="wrap_content">

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="53dp"
    android:gravity="center"
    android:text="Add New Blood Sample"
    android:textColor="#000"
    android:textSize="20dp"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView13"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Please enter the Name and Gender"
    android:textColor="#000"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView1" />

<EditText
    android:id="@+id/samplename_input"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_margin="12dp"
    android:ems="10"
    android:hint="Name"
    android:inputType="textMultiLine"
    android:textColor="@color/white"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView13" />

<EditText
    android:id="@+id/gender_input"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_margin="12dp"
    android:ems="10"
    android:hint="Age"
    android:inputType="textMultiLine"
    android:textColor="@color/white"
    app:layout_constraintBottom_toTopOf="@+id/submit_button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/samplename_input" />

<Button
    android:id="@+id/submit_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Submit"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/cancel_button"
    app:layout_constraintStart_toStartOf="parent" />

<Button
    android:id="@+id/cancel_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Cancel"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/submit_button" />

</androidx.constraintlayout.widget.ConstraintLayout>

相关问题