这里是在TimePickerFragment,DialogFragment的帮助下使用imageview选择到达和退出时间。 我的问题是如何选择退出时间..当选择退出时间时,它将仅显示在到达时间文本框中。如何解决这个问题对我有帮助。
这里是到达和退出时间的代码 我使用了两个文本框和两个图像视图按钮。
import java.util.Calendar;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TimePicker;
@SuppressLint("ValidFragment")
public class MainActivity extends FragmentActivity {
EditText arrtime,exittime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void selectArrivalTime(View view) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "TimePicker");
}
public void selectExitTime(View view) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "TimePicker");
}
public void populateSetArrTime(int hourOfDay, int minute) {
arrtime = (EditText)findViewById(R.id.editText1);
arrtime.setText(+hourOfDay+":"+minute);
}
public void populateSetExitTime(int hourOfDay, int minute) {
exittime= (EditText)findViewById(R.id.editText2);
exittime.setText(+hourOfDay+":"+minute);
}
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
populateSetArrTime(hourOfDay, minute);
}
public void onTimeSet1(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
populateSetExitTime(hourOfDay, minute);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是我的XML布局。
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="67dp"
android:text="Arrival time" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/editText1"
android:layout_marginTop="30dp"
android:text="Exit time" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView1"
android:layout_toRightOf="@+id/textView1"
android:clickable="false"
android:ems="10"
android:focusable="false"
android:focusableInTouchMode="false"
android:inputType="time" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/editText1"
android:clickable="false"
android:ems="10"
android:focusable="false"
android:focusableInTouchMode="false"
android:inputType="time" >
</EditText>
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/editText2"
android:layout_marginTop="51dp"
android:contentDescription="@string/selectarrtime"
android:onClick="selectArrivalTime"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editText2"
android:layout_alignTop="@+id/imageButton1"
android:layout_marginRight="33dp"
android:contentDescription="@string/selectexittime"
android:onClick="selectExitTime"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
如果选择到达时间,我将在我的到达编辑文本框中显示时间,如果我选择退出时间,它将显示在我的退出编辑文本框中未显示的同一到达编辑文本框中的时间如何解决此问题提前谢谢你。
答案 0 :(得分:0)
@SuppressLint("ValidFragment")
public class MainActivity extends FragmentActivity {
EditText arrtime,exittime;
static type=""; //changed
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton b1=(ImageButton)findViewById(R.id.imageButton1); //changed from here
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setTime("arrival");
}
});
ImageButton b2=(ImageButton)findViewById(R.id.imageButton2);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setTime("exit");
}
}); ///to here
}
public void setTime(String type) { //function changed
MainActivity.type=type;
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "TimePicker");
}
public void populateTime(int hourOfDay, int minute) { //function changed
if(type.equals("arrival") {
arrtime = (EditText)findViewById(R.id.editText1);
arrtime.setText(+hourOfDay+":"+minute);
}
else if(type.equals("exit") {
arrtime = (EditText)findViewById(R.id.editText2);
exittime.setText(+hourOfDay+":"+minute);
}
}
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
populateTime(hourOfDay, minute); //changed
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
同时从xml文件中的ImageButtons中删除行android:onClick行