如何访问布局对话框中的项目?

时间:2018-12-24 22:18:22

标签: java android

对话框是由XML层设计的,其内部的布局包含一个微调器和TextView的集合,并且从主活动类中调用了一些按钮,现在我想知道其中的哪个微调器。 D1()函数 我想在图层布局中定义元素,该怎么做? //layout.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="260dp"
        android:layout_height="200dp"
        android:orientation="vertical"
        android:background="#f71717">

        <LinearLayout
            android:id="@+id/l_layout"
            android:paddingTop="10dp"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            tools:ignore="ObsoleteLayoutParam">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textColor="#ffffff"
                android:text="وقت التسليم"
                android:textSize="20dp"
                tools:ignore="HardcodedText,SpUsage" />

            <Spinner
                android:id="@+id/tex"
                android:layout_width="172dp"
                android:paddingRight="40dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textView"
                android:drawSelectorOnTop="true"
                android:popupBackground="#c853d7"
                style="@style/spinner_style"
                tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry,SpUsage"
                android:entries="@array/day_"/>
        </LinearLayout>


        <RelativeLayout

            android:id="@+id/rl"

            android:layout_width="250dp"
            android:layout_height="129dp"
            android:layout_below="@+id/l_layout"
            android:background="#f71717"
           android:layout_marginTop="0dp"
            tools:ignore="ObsoleteLayoutParam">


            <TextView
                android:id="@+id/text_h"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_toStartOf="@+id/spinner_minutes2"
                android:paddingLeft="10dp"
                android:text="ساعة"
                tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry" />

            <TextView
                android:id="@+id/text_m"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/spinner_minutes2"
                android:layout_alignBottom="@+id/spinner_minutes2"
                android:layout_alignStart="@+id/button_holder"
                android:paddingLeft="10dp"
                android:text="دق"
                tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry" />

            <TextView
                android:id="@+id/text_pam"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="30dp"
                android:text="ص/م"
                tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry"
                android:layout_marginEnd="12dp"
                android:layout_alignBaseline="@+id/spinner_minutes"
                android:layout_alignBottom="@+id/spinner_minutes"
                android:layout_alignEnd="@+id/spinner_minutes3" />


            <Spinner
                android:id="@+id/spinner_minutes"
                android:layout_width="85dip"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                style="@style/spinner_style"
                android:layout_alignStart="@+id/spinner_minutes2"
                android:entries="@array/fruits" />


            <Spinner
                android:id="@+id/spinner_minutes2"
                android:layout_width="85dip"
                android:layout_height="wrap_content"
                android:layout_below="@+id/spinner_minutes"
                android:layout_marginStart="16dp"
                style="@style/spinner_style"
                android:layout_toEndOf="@+id/text_m"
                android:entries="@array/fruits" />


            <Spinner
                android:id="@+id/spinner_minutes3"
                android:layout_width="85dip"
                android:layout_height="wrap_content"
                style="@style/spinner_style"
                android:paddingRight="20dp"
                android:entries="@array/apm"
                tools:ignore="RtlHardcoded,RtlSymmetry"
                android:layout_alignBaseline="@+id/spinner_minutes2"
                android:layout_alignBottom="@+id/spinner_minutes2"
                android:layout_toEndOf="@+id/spinner_minutes" />

            <TextView
                android:id="@+id/text_timer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:visibility="gone" />

            <LinearLayout
                android:id="@+id/button_holder"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/spinner_minutes"
                android:layout_centerHorizontal="true"
                android:paddingTop="10dp"
                android:layout_marginTop="20dip">

                <Button
                    android:id="@+id/button_set"
                    android:layout_width="100dip"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="5dip"
                    android:layout_marginLeft="10dip"
                    android:text="Set"
                    tools:ignore="ButtonStyle,HardcodedText,RtlHardcoded" />

                <Button
                    android:id="@+id/button_cancel"
                    android:layout_width="100dip"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="5dip"
                    android:layout_marginRight="10dip"
                    android:text="Cancel"
                    tools:ignore="ButtonOrder,ButtonStyle,HardcodedText,RtlHardcoded" />
            </LinearLayout>


        </RelativeLayout>


    </LinearLayout>

    // class: mainactivity

    public class MainActivity extends AppCompatActivity {


        Button buttonstartSetDialog;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog);
            buttonstartSetDialog.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    d1();
                }});

        }

        public void  d1(){
            Dialog about_dlg = new Dialog(MainActivity.this);
            about_dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);
            about_dlg.setContentView(R.layout.layout);
            about_dlg.show();

            //Spinner spinner = (Spinner) findViewById(R.id.spinner);

        }


    }

1 个答案:

答案 0 :(得分:1)

访问版式中的Spinner

public void  d1(){
            Dialog about_dlg = new Dialog(MainActivity.this);
            about_dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);
            about_dlg.setContentView(R.layout.layout);
            Spinner sp1 = about_dlg.findViewById(R.id.spinner_minutes);
            Spinner sp1 = about_dlg.findViewById(R.id.spinner_minutes);
            Spinner sp1 = about_dlg.findViewById(R.id.spinner_minutes);
            about_dlg.show();
        }