gpt4 book ai didi

Android:如何使用 onFocusChangeListener 启动 DatePickerDialog?

转载 作者:行者123 更新时间:2023-11-29 17:01:34 25 4
gpt4 key购买 nike

我在用户输入表单上有一个 EditText 行。单击 EditText 以启动 DatePickerDialog 使用 setOnTouchListener 效果很好。我设置的用于处理 EditText 上的 OnFocusChangesetonFocusChangeListener 不起作用。我在这里缺少什么?

(不确定这是否有帮助,但在早期版本中我使用了 DatePickerFragment 并且 onFocusChange 工作正常......所以问题可能在于使用 DatePickerDialog 而不是 Fragment?我用 onFocusChange() 启动 Fragment 的两行代码是:

DatePickerFragment newFragment = new DatePickerFragment(); // and
newFragment.show(getSupportFragmentManager(), "datePicker");)

Activity .java

public class Activity extends AppCompatActivity implements OnFocusChangeListener {
...
private DatePickerDialog zDatePickerDialog
private int mYear;
private int mMonth;
private int mDay;

Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

zListenerEditText = (ListenerEditText) findViewById(id.ZEditText);

Calendar calendar = Calendar.getInstance();
mYear = calendar.get(Calendar.YEAR);
mMonth = calendar.get(Calendar.MONTH);
mDay = calendar.get(Calendar.DAY_OF_MONTH);
mDatePickerDialog = new DatePickerDialog(this,this,mYear,mMonth,mDay);

// This is working fine to handle the a click to launch the DatePickerDialog
zListenerEditText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// launch the DatePickerDialog.
mDatePickerDialog.show();
return true;
}
});

// This does not work to launch the DatePickerDialog
zListenerEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus ) {
// launch the DatePickerDialog.
mDatePickerDialog.show();
...
});
}
}

Activity .xml

    ...
<com.widget.ListenerEditText
android:id="@+id/ZEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:textColor="#FFFFFF" />

在Activity文件中编辑1:

...
@Override
public void onFocusChange(View v, boolean hasFocus) {
switch (v.getId()) {
case id.CEditText:
Toast.makeText(this,"C",Toast.LENGTH_LONG).show();
// Handle onFocusChange for editText1
break;
case id.DEditText:
Toast.makeText(this,"D",Toast.LENGTH_LONG).show();
// Handle onFocusChange for editText2
break;
case id.EEditText:
Toast.makeText(this,"E",Toast.LENGTH_LONG).show();
// Handle onFocusChange for editText3
break;
case id.FEditText:
Toast.makeText(this,"F",Toast.LENGTH_LONG).show();
// Handle onFocusChange for editText4
break;
case id.GEditText:
Toast.makeText(this,"G",Toast.LENGTH_LONG).show();
// Handle default scenario.
break;
}
}

最佳答案

从这里删除 OnFocusChangeListener public class Activity extends AppCompatActivity implements OnFocusChangeListener

保持这样

public class Activity extends AppCompatActivity {
zListenerEditText = (ListenerEditText) findViewById(id.ZEditText);

Calendar calendar = Calendar.getInstance();
mYear = calendar.get(Calendar.YEAR);
mMonth = calendar.get(Calendar.MONTH);
mDay = calendar.get(Calendar.DAY_OF_MONTH);
mDatePickerDialog = new DatePickerDialog(this,this,mYear,mMonth,mDay);

// This is working fine to handle the a click to launch the DatePickerDialog

// This does not work to launch the DatePickerDialog
zListenerEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus ) {
// launch the DatePickerDialog.
mDatePickerDialog.show();
...
});
}

关于Android:如何使用 onFocusChangeListener 启动 DatePickerDialog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42592209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com