[英]EditText & onFocusChangeListener
1st of all I wanna thank you all for your great help. 首先,我要感谢你们的大力帮助。 I'm a newby and stackoverflow answered most of my questions with already existing answers :-) 我是一个新手,stackoverflow用我已经存在的答案回答了我的大多数问题:-)
For my current project I want to create a kind of a calendar entry and the user has to set a date and time for this entry. 对于我当前的项目,我想创建一种日历条目,用户必须为此条目设置日期和时间。 I decided to have EditText and want to open the picker @OnFocus on EditText (I know about the discussions if this makes sense or not but I think it makes sense to do so). 我决定使用EditText并想在EditText上打开选择器@OnFocus(我知道讨论是否有意义,但我认为这样做是有意义的)。
I have two issues now: 我现在有两个问题:
1) I don't get the OnFocusListener to work :-( 1)我没有让OnFocusListener工作:-(
2) The time picker is always am/pm format though in the EditText it writes 24h format. 2)时间选择器总是am / pm格式,但在EditText中它写入24h格式。 My device is set to 24h and I don't get the time picker to use 24h, dependent on the device settings. 我的设备设置为24小时,我没有让时间选择器使用24小时,具体取决于设备设置。 I know it can be set to always be 24h but I think this is confusing for other countries not using 24h so it should be device setting dependent. 我知道它可以设置为总是24小时,但我认为这对其他不使用24小时的国家来说很困惑,因此应该依赖于设备设置。
I hope you can help me and fix my hopefully not so messy code. 我希望你能帮助我并修复我希望不那么混乱的代码。 Thanks in advance! 提前致谢!
Here's my code: 这是我的代码:
public class Test extends ActionBarActivity implements OnFocusChangeListener {
EditText editText, txtDate, txtTime;
Button button, btnDate;
// Variable for storing current date and time
private int mYear, mMonth, mDay, mHour, mMinute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
// Force keyboard to open as request focus doesn’t open keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
// Date & Time Picker Stuff
txtDate = (EditText) findViewById(R.id.etDate);
txtTime = (EditText) findViewById(R.id.etTime);
txtDate.setOnFocusChangeListener(new OnFocusChangeListener());
txtTime.setOnFocusChangeListener(new OnFocusChangeListener());
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (v == txtDate) {
// Process to get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
DatePickerDialog dpd = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// Display Selected date in edit text
txtDate.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
dpd.show();
}
if (v == txtTime) {
// Process to get Current Time
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog tpd = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
// Display Selected time in edit text
txtTime.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
tpd.show();
}
}
To fix focus listener issue: 要修复焦点侦听器问题:
Change setOnFocusChangeListener(new OnFocusChangeListener());
更改setOnFocusChangeListener(new OnFocusChangeListener());
to setOnFocusChangeListener(this);
to setOnFocusChangeListener(this);
to hook your EditTexts
up to the method you defined. 将EditTexts
挂钩到您定义的方法。
A solution to 12 hour vs 24 hour display: 12小时与24小时显示的解决方案:
You can set the display to 24 hour if the system is set to 24 hour, or to 12 hour if the system is set to 12 hour formatting. 如果系统设置为24小时,则可以将显示设置为24小时;如果系统设置为12小时格式,则可以将显示设置为12小时。
import android.text.format.DateFormat;
// ...
timePicker.setIs24HourView(DateFormat.is24HourFormat(ActionBarActivity.this))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.