EditText& onFocusChangeListener

时间:2014-05-08 14:55:02

标签: android


首先,我要感谢你们的大力帮助。我是一个新手,stackoverflow用已经存在的答案回答了我的大部分问题: - )

对于我当前的项目,我想创建一种日历条目,用户必须为此条目设置日期和时间。 我决定使用EditText并想在EditText上打开选择器@OnFocus(我知道讨论是否有意义,但我认为这样做是有意义的。)

我现在有两个问题:
1)我没有让OnFocusListener工作:-(
2)时间选择器总是am / pm格式,但在EditText中它写入24h格式。我的设备设置为24小时,我没有让时间选择器使用24小时,具体取决于设备设置。我知道它可以设置为总是24小时,但我认为这对其他不使用24小时的国家来说很困惑,所以应该依赖设备设置。

我希望你能帮助我并修复我希望不那么混乱的代码。 提前谢谢!

这是我的代码:

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();
    }
 }

1 个答案:

答案 0 :(得分:2)

修复焦点侦听器问题:

setOnFocusChangeListener(new OnFocusChangeListener());更改为setOnFocusChangeListener(this);,将您的EditTexts挂钩到您定义的方法。

12小时与24小时显示的解决方案:

如果系统设置为24小时,您可以将显示设置为24小时;如果系统设置为12小时格式,则可以将显示设置为12小时。

import android.text.format.DateFormat;

// ...

timePicker.setIs24HourView(DateFormat.is24HourFormat(ActionBarActivity.this))