我如何在动态布局中打开时间选择器对话框

问题描述 投票:0回答:1

我有三个编辑文本1)服务2)是收费3)是时间所以我不知道会给美容院提供多少服务。所以我制作了一个动态布局,服务提供商动态地选择了那里的服务。我使用的是两个布局,一个是父布局,第二个是子视图布局..所有工作很好,但是要选择时间。时间选择器对话框仅在父布局中打开。无法在子视图布局中打开。如果有人知道,请帮助我。这是我完整的代码和图片

childview.xml

?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    >

    <EditText
        android:id="@+id/service"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:hint="@string/edittext_service"
        android:inputType="text"/>
    <EditText
        android:id="@+id/charges"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:hint="@string/edittext_charge"
        android:inputType="number"/>
    <EditText
        android:id="@+id/time"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:hint="@string/edittext_time"

        android:inputType="number"/>
    <Button
        android:id="@+id/Sub"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:background="@android:drawable/ic_delete"
        android:onClick="onDelete"/>

</LinearLayout>

ServicesAndRateList.xml

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="false">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="16dp"
        android:id="@+id/parent_linear_layout"
        tools:context=".ServicesAndRateList">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/service"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:hint="@string/edittext_service"
                android:inputType="text"/>
            <EditText
                android:id="@+id/charges"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:hint="@string/edittext_charge"
                android:inputType="number"/>
            <EditText
                android:id="@+id/time"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:hint="@string/edittext_time"

                android:inputType="number"/>
            <Button
                android:id="@+id/Sub"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:onClick="INSERT"
                android:background="@android:drawable/ic_delete"
                />


        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dp"
            android:orientation="vertical">

            <Button
                android:id="@+id/add"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/ripple"
                android:layout_gravity="center"
                android:onClick="onAddField"
                android:textColor="#000"
                android:text="Add Field"
                android:paddingLeft="5dp"/>
            <Button
                android:id="@+id/submit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/ripple"
                android:layout_gravity="center"
                android:onClick="INSERT"
                android:textColor="#000"
                android:text="Register"
                android:paddingLeft="5dp"/>


        </LinearLayout>


    </LinearLayout>

</ScrollView>

ServicesAndRateList.java

public class ServicesAndRateList extends AppCompatActivity {

EditText service, charge, time;
TimePickerDialog timePickerDialog;
private LinearLayout parentLinearLayout;
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_services_and_rate_list);

    parentLinearLayout = (LinearLayout) findViewById(R.id.parent_linear_layout);

    time = findViewById(R.id.time);
    button = (Button) findViewById(R.id.submit);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            for (int i = 0; i < parentLinearLayout.getChildCount() - 1; i++) {
                service = (EditText) (parentLinearLayout.getChildAt(i).findViewById(R.id.service));
                charge = (EditText) (parentLinearLayout.getChildAt(i).findViewById(R.id.charges));
                time = (EditText) (parentLinearLayout.getChildAt(i).findViewById(R.id.time));

                String Service = service.getText().toString();
                String Charge = charge.getText().toString();
                String Time = time.getText().toString();

                if (TextUtils.isEmpty(Service)) {
                    service.requestFocus();
                } else if (TextUtils.isEmpty(Charge)) {
                    charge.requestFocus();
                } else if (TextUtils.isEmpty(Time)) {
                    time.requestFocus();
                }
            }
        }
    });


    time.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            timePickerDialog = new TimePickerDialog(ServicesAndRateList.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int hourOfDay, int minutes) {

                    time.setText(hourOfDay + ":" + minutes);

                }
            }, 0, 0, true);

            timePickerDialog.show();
        }
    });

}


public void onDelete(View view) {
    parentLinearLayout.removeView((View) view.getParent());
}

public void onAddField(View view) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View rowView = inflater.inflate(R.layout.childview, null);
    // Add the new row before the add field button.
    parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);

}

}

我的xml图片

enter image description here

android timepicker
1个回答
0
投票

问题是您仅将View.ClickListener设置为作为EditText time的直接子级的单个视图R.layout.activity_services_and_rate_list

稍后您将更多的子视图添加到膨胀的R.layout.activity_services_and_rate_list布局中,但未设置点击侦听器。

其中一个选项是创建一个方法,该方法将为每个经过的时间EditText设置点击侦听器。

注:考虑将RecyclerView与适配器一起使用。当布局中的视图数增加时,它的性能会好得多。

public class ServicesAndRateList extends AppCompatActivity {

    //your variables here ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //your other onCreate code here ...

        setTimeEditTextClickListener(time);
    }

    private void setTimeEditTextClickListener(EditText time) {
        time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                timePickerDialog = new TimePickerDialog(ServicesAndRateList.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker timePicker, int hourOfDay, int minutes) {
                        time.setText(hourOfDay + ":" + minutes);
                    }
                }, 0, 0, true);

                timePickerDialog.show();
            }
        });
    }

    public void onAddField(View view) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View rowView = inflater.inflate(R.layout.childview, null);
        // Add the new row before the add field button.
        parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
        setTimeEditTextClickListener((EditText) rowView.findViewById(R.id.time));
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.