我如何在动态布局中打开时间选择器对话框
how i can open time picker dialog box in dynamic layout
我有三个编辑文本
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);
}
}
picture of my xml
问题是您仅将 View.ClickListener
设置为 R.layout.activity_services_and_rate_list
的直接子视图 EditText time
。
稍后您将更多的子视图添加到膨胀的 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));
}
}
我有三个编辑文本 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);
}
}
picture of my xml
问题是您仅将 View.ClickListener
设置为 R.layout.activity_services_and_rate_list
的直接子视图 EditText time
。
稍后您将更多的子视图添加到膨胀的 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));
}
}