如何从 DatePickerDialog 的值设置文本
how to setText from value of DatePickerDialog
我试图在单击按钮并在 TextView 上设置其值后选择日期。
此线程帮助 Datepicker: How to popup datepicker when click on button and store value in variable
但是我无法在 setText 上设置日期选择器的值,textview 对象 v returns 空空指针异常。
v.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("/").append(mDay).append("/")
.append(mYear).append(" "));
System.out.println(v.getText().toString());
感谢任何帮助
完整代码:
Mainactivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void pick(View v){
Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
System.out.println("the selected " + mDay);
DatePickerDialog dialog = new DatePickerDialog(MainActivity.this,
new mDateSetListener(), mYear, mMonth, mDay);
dialog.show();
}
}
class mDateSetListener implements DatePickerDialog.OnDateSetListener {
private TextView v;
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
// getCalender();
int mYear = year;
int mMonth = monthOfYear;
int mDay = dayOfMonth;
v.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("/").append(mDay).append("/")
.append(mYear).append(" "));
}
}
xml 文件:
<RelativeLayout
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="com.example.datesamp.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="101dp"
android:text="Pick date"
android:onClick="pick"/>
<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
/>
</RelativeLayout>
您需要先初始化变量 v,然后再在 onDateSet() 中使用它,例如在 v.setText(...):
之前添加以下语句
v = (TextView) findViewById(R.id.t1);
v.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("/").append(mDay).append("/")
.append(mYear).append(" "));
但是为了在 onDateSet() 中插入这条语句,我建议你将 class mDateSetListener 定义为内部 class inside MainActivity,它会让你的生活更轻松。
我试图在单击按钮并在 TextView 上设置其值后选择日期。 此线程帮助 Datepicker: How to popup datepicker when click on button and store value in variable
但是我无法在 setText 上设置日期选择器的值,textview 对象 v returns 空空指针异常。
v.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("/").append(mDay).append("/")
.append(mYear).append(" "));
System.out.println(v.getText().toString());
感谢任何帮助
完整代码: Mainactivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void pick(View v){
Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
System.out.println("the selected " + mDay);
DatePickerDialog dialog = new DatePickerDialog(MainActivity.this,
new mDateSetListener(), mYear, mMonth, mDay);
dialog.show();
}
}
class mDateSetListener implements DatePickerDialog.OnDateSetListener {
private TextView v;
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
// getCalender();
int mYear = year;
int mMonth = monthOfYear;
int mDay = dayOfMonth;
v.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("/").append(mDay).append("/")
.append(mYear).append(" "));
}
}
xml 文件:
<RelativeLayout
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="com.example.datesamp.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="101dp"
android:text="Pick date"
android:onClick="pick"/>
<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
/>
</RelativeLayout>
您需要先初始化变量 v,然后再在 onDateSet() 中使用它,例如在 v.setText(...):
之前添加以下语句v = (TextView) findViewById(R.id.t1);
v.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("/").append(mDay).append("/")
.append(mYear).append(" "));
但是为了在 onDateSet() 中插入这条语句,我建议你将 class mDateSetListener 定义为内部 class inside MainActivity,它会让你的生活更轻松。