首先我经历了一些基本的android教程,我尝试做DatePicker应用程序。当我选择日期时,所选日期将显示在第一个DateView下面的第一个TextView中。它对我来说很好。但是我需要再添加一个DatePicker。当我选择第二个DatePicker时,如果我给我的选择已完成,那么它会更改第一个TextView。我需要在第二个TextView中进行更改。请帮我解决这个问题。
这是我的xml文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="414dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="66dp"
android:orientation="vertical" >
<Button
android:id="@+id/btnChangeDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Date" />
<TextView
android:id="@+id/tvDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<DatePicker
android:id="@+id/dpResult"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnChangeDate1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="End Date" />
<DatePicker
android:id="@+id/dpResult1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tvDate1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
这是我的活动代码。
public class MainActivity extends Activity {
private TextView tvDisplayDate,tvDate1;
private DatePicker dpResult,dpResult1;
private Button btnChangeDate,btnChangeDate1;
private int year;
private int month;
private int day;
static final int DATE_DIALOG_ID = 999;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setCurrentDateOnView();
addListenerOnButton();
setCurrentDateOnView1();
addListenerOnButton1();
}
// display current date
public void setCurrentDateOnView() {
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
dpResult = (DatePicker) findViewById(R.id.dpResult);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
tvDisplayDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
// set current date into datepicker
dpResult.init(year, month, day, null);
}
public void addListenerOnButton() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
public void setCurrentDateOnView1() {
tvDate1 = (TextView) findViewById(R.id.tvDate);
dpResult1 = (DatePicker) findViewById(R.id.dpResult1);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
tvDate1.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
// set current date into datepicker
dpResult1.init(year, month, day, null);
}
public void addListenerOnButton1() {
btnChangeDate1 = (Button) findViewById(R.id.btnChangeDate1);
btnChangeDate1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener,
year, month,day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener
= new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// set selected date into textview
tvDisplayDate.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
// set selected date into datepicker also
dpResult.init(year, month, day, null);
}
};
提前致谢。
答案 0 :(得分:0)
在此功能tvDate1 = (TextView) findViewById(R.id.tvDate);
public void setCurrentDateOnView1()
行
您创建了一个变量tvdate
但是您使用了第一个TextView
的ID,因此请更改此
tvDate1 = (TextView) findViewById(R.id.tvDate);
到
tvDate1 = (TextView) findViewById(R.id.tvDate1);
修改强>
你只有一个监听器,他使用变量tvDisplayDate
。因此,将日期设置为第一个TextView
。你需要使用2个监听器而不是两个监听器。
答案 1 :(得分:0)
添加另一个对话框ID,如:
static final int DATE_DIALOG_ID_2 = 998;
并添加第二个按钮,如:
tvDisplayDate1 = (TextView) findViewById(R.id.tvDate1);
然后在你的第二个按钮上执行此操作:
btnChangeDate1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID_2 );
}
});
然后:
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener,
year, month,day);
case DATE_DIALOG_ID_2:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener2,
year, month,day);
}
return null;
}
然后
private DatePickerDialog.OnDateSetListener datePickerListener2
= new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// set selected date into textview
tvDisplayDate1.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
// set selected date into datepicker also
dpResult1.init(year, month, day, null);
}
};
希望这有帮助