我正在一个用户在TimePickerDialog中输入时间的项目中工作。我想获取该输入并将其存储在另一个变量中并将其显示为textView。
public class MainActivity extends Activity {
Button b1;
TextView tv;
int hour,min;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.b1);
tv = (TextView)findViewById(R.id.tv1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TimePickerDialog tpd = new TimePickerDialog(MainActivity.this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
hour = hourOfDay;
min = minute;
}
}, hour, min, false);
tpd.show();
tv.setText(hour + ":" + min);
}
});
}
}
当我首先运行此应用程序时,textview显示0:0。我按下按钮对话框出现我设置时间并按完成但文本视图没有改变,只有在我再次按下按钮后,textview才会改变到上一个输入。我想要的是,当我在timepickerdialog中按完后,textview将显示我设置的时间。只有在我再次按下按钮后才能显示该值。
答案 0 :(得分:3)
单击完成按钮后,您需要更新TextView
。现在,您的TextView
在完成时间选择后无法更新。只需更改您的代码,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.b1);
tv = (TextView)findViewById(R.id.tv1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TimePickerDialog tpd = new TimePickerDialog(MainActivity.this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
hour = hourOfDay;
min = minute;
//Place it here
tv.setText(hour + ":" + min);
}
}, hour, min, false);
tpd.show();
}
});
}
}
答案 1 :(得分:0)
试试这个
public class MainActivity extends Activity {
Button b1;
TextView tv;
int hour,min;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.b1);
tv = (TextView)findViewById(R.id.tv1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TimePickerDialog tpd = new TimePickerDialog(MainActivity.this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
hour = hourOfDay;
min = minute;
tv.setText(hour + ":" + min);
}
}, hour, min, false);
tpd.show();
}
});
}
}