gpt4 book ai didi

android - 单击编辑文本时应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 19:45:28 25 4
gpt4 key购买 nike

当我点击时间或日期编辑文本时,时间选择器和日期选择器均未出现,应用程序崩溃,在 logcat 中出现:

Could not find method DatePickerDialog(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatEditText with id 'input2'

提前致谢

这是我的 Activity

public class MainActivity3 extends AppCompatActivity implements
View.OnClickListener {


EditText txtDate, txtTime;
private int mYear, mMonth, mDay, mHour, mMinute;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.to_do_dialog_view);

txtDate=(EditText)findViewById(R.id.input2);
txtDate.setOnClickListener(this);

txtTime=(EditText)findViewById(R.id.input3);
txtTime.setOnClickListener(this);



}


public void onClick(View v) {

if (v == txtDate) {

// 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);


DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {

txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);

}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
if (v == txtTime) {

// 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 timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {

txtTime.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
timePickerDialog.show();
}
}
}

to_do_dialog_view 布局

  <EditText
android:id="@+id/input2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:singleLine="true"
android:imeOptions="actionDone"
android:hint="Date"
android:inputType="date"
android:onClick="DatePickerDialog"
/>

<EditText
android:id="@+id/input3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:singleLine="true"

android:hint="Time"

android:onClick="TimePickerDialog"
/>

</LinearLayout>
</LinearLayout>

最佳答案

从 XML 中删除 android:onClick="TimePickerDialog"android:onClick="DatePickerDialog"

像您已经完成的那样,从 Java 代码设置 onClickListener


这不是必需的,如果您执行上述操作

错误表明您缺少两种方法。

public void DatePickerDialog(View v) {}

public void TimePickerDialog(View v) {}

关于android - 单击编辑文本时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37817236/

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