我正在使用TabHost和片段创建一个带有标签界面的应用程序,用户需要使用TimePickerDialog选择时间,并将选定的时间填充到EditText视图中,因此我将时间选择器移动到片段中我可以在多个选项卡中使用它,但是问题是当我使用对话框设置时间时,填充的EditText视图是最后一个选项卡中的那个,而不是打开TimePickerDialog的那个,我该如何处理? / p>
这是时间选择器片段的布局
#include "/root/dev/Practice/include/FileWriter.h"
#include <fstream>
// g++ linker error if 'inline' not included - why?
inline FileWriter::FileWriter(std::string fileName)
{
this->fileLocation = fileName;
const char * x = this->fileLocation.c_str();
this->filestream = new std::ofstream();
this->filestream->open(x, std::ios::out | std::ios::app);
}
inline FileWriter::~FileWriter()
{
this->filestream->close();
}
inline void FileWriter::AddLine(std::string line)
{
*this->filestream << line << std::endl;
}
以下是填充EditText视图的代码,该视图存储在 MainActivity.java
中<RelativeLayout 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"
tools:context="com.ztech.fakecallm.TimePickerFragment">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="time"
android:ems="10"
android:id="@+id/timePicker"
android:focusable="false"
android:hint="@string/time"
android:onClick="onClickTime"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
这是代表第一个标签的片段布局
public void onClickTime(View view) {
final Calendar c = Calendar.getInstance();
int cHour = c.get(Calendar.HOUR_OF_DAY);
int cMinute = c.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
scheduleDate = c.getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm a", Locale.US);
EditText timeInput = (EditText)findViewById(R.id.timePicker);
timeInput.setText(dateFormat.format(scheduleDate));
}
}, cHour, cMinute, false);
timePickerDialog.show();
}
和代表第二个标签的片段的布局
<RelativeLayout 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"
tools:context="com.ztech.fakecallm.CallsFragment">
<fragment
android:id="@+id/contact_picker_frag"
class="com.ztech.fakecallm.ContactPickerFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_contact_picker"/>
<RadioGroup android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:id="@+id/radioGroup"
android:layout_marginTop="137dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/incoming"
android:id="@+id/incomingRadioButton"
android:checked="true"
android:layout_margin="20dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/outgoing"
android:id="@+id/outgoingRadioButton"
android:checked="false"
android:layout_margin="20dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/missed"
android:id="@+id/missedRadioButton"
android:checked="false"
android:layout_margin="20dp"/>
</RadioGroup>
<fragment
android:id="@+id/time_picker_fragment_calls"
class="com.ztech.fakecallm.TimePickerFragment"
tools:layout="@layout/fragment_time_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/radioGroup"
android:layout_alignParentStart="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/schedule"
android:id="@+id/setCallScheduleButton"
android:layout_margin="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
毕竟 MainActivity 看起来像这样
<RelativeLayout 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"
tools:context="com.ztech.fakecallm.SMSFragment">
<fragment
android:id="@+id/contact_picker_frag_sms"
class="com.ztech.fakecallm.ContactPickerFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_contact_picker"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="150dp"
android:id="@+id/message_input"
android:gravity="top"
android:hint="@string/message"
android:layout_marginTop="140dp"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:id="@+id/radioGroup"
android:layout_below="@+id/message_input"
android:layout_alignParentStart="true">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/inbox"
android:id="@+id/inbox_radio_button"
android:checked="true"
android:layout_margin="20dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sent"
android:id="@+id/sent_radio_button"
android:checked="false"
android:layout_margin="20dp"/>
</RadioGroup>
<fragment
android:id="@+id/time_picker_fragment_sms"
class="com.ztech.fakecallm.TimePickerFragment"
tools:layout="@layout/fragment_time_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/radioGroup"
android:layout_alignParentStart="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/schedule"
android:id="@+id/setSMSScheduleButton"
android:layout_margin="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
答案 0 :(得分:0)
使用此
dependencies {
compile 'com.wdullaer:materialdatetimepicker:2.3.0'
}
和
将您的片段实施为OnTimeSetListener
并在片段中显示TimePickerDialog
Calendar now2 = Calendar.getInstance();
TimePickerDialog tpd = TimePickerDialog.newInstance(
NewActivity.this, now2.get(Calendar.HOUR_OF_DAY), now2.get(Calendar.MINUTE),
now2.get(Calendar.SECOND), false
);
tpd.show(getFragmentManager(), "Select Time");
并且
@Override
public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
String time = "You picked the following time: "+hourOfDay+"h"+minute;
timeTextView.setText(time);
}