gpt4 book ai didi

java - TextView onClcikclistener 似乎不起作用

转载 作者:行者123 更新时间:2023-12-02 12:39:55 25 4
gpt4 key购买 nike

当我单击应用程序中的文本时,此 TextView 上的 onClickListener 似乎不起作用,它不显示 TimePickerDialog 或任何内容。我应该使用 TouchListener 吗?有什么问题?请帮忙提前致谢--Emanuel。

这是代码:

hora_text.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
hora_button.setOnClickListener(
new Button.OnClickListener() {
public void onClick (View v){
Calendar cal = Calendar.getInstance();
int hora = cal.get(Calendar.HOUR);
int minutos = cal.get(Calendar.MINUTE);
TimePickerDialog mTimePicker = new TimePickerDialog(getActivity(),TimePickerDialog.THEME_HOLO_LIGHT,hourSetListener,hora,minutos,true);
mTimePicker.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mTimePicker.show();
}
});

hourSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hora, int minutos) {
hora_text.setText(hora+":"+minutos);
}
};
}
});

date_text.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
date_button.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog date_dialog = new DatePickerDialog(getActivity(),
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
dateSetListener,
year, month, day);
date_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
date_dialog.show();
};
}
);

dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month=month + 1;
String dt = day + "-" + month + "-" + year;
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
try {
sdf.parse(dt);
date_text.setText(dt);
} catch (ParseException e) {
e.printStackTrace();
}
}
};
}
}
);

最佳答案

就像@PPartisan 在评论中提到的那样,您并没有真正设置 TextView 可点击的功能,因为您是在按钮中设置功能。所以在你的情况下,它应该是:

hora_text.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
int hora = cal.get(Calendar.HOUR);
int minutos = cal.get(Calendar.MINUTE);
TimePickerDialog mTimePicker = new TimePickerDialog(getActivity(),TimePickerDialog.THEME_HOLO_LIGHT,hourSetListener,hora,minutos,true);
mTimePicker.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mTimePicker.show();
}
);

关于java - TextView onClcikclistener 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44987328/

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