gpt4 book ai didi

java - Android onClick(View) 与 switch 不正确的情况

转载 作者:行者123 更新时间:2023-12-02 06:30:42 24 4
gpt4 key购买 nike

我有一个 OnClickListener 实例来将点击定向到多个其他处理程序方法,通常:

void onClick(View v) {
switch (v.getId()) {
case R.id.viewA:
handleButtonA();
break;
case R.id.viewB:
handleButtonB();
break;
default:
handleDefaultCondition();
break;
}
}

当我单击 View A 时,将调用 handleButtonA()。当我单击 View B 时,没有任何反应。当我单击任何其他 View 时,它会调用handleDefaultCondition()。我对一系列 if/else if 语句也有同样的行为,所以我不认为该问题与我使用 switch 有关>.

完整代码如下:

public class MyClickListener implements View.OnClickListener {
public void onClick(View view) {
int viewId = view.getId();
switch (viewId) {
case R.id.time_frag_mark_button:
handleMarkButton(); //just calls dummy method with a Log statement inside for testing
break;
case R.id.finalize_entry_button:
handleFinalizeButton(); //just calls dummy method with a Log statement inside for testing
break;
default:
openTimePickerDialog(view.getId()); //just calls dummy method with a Log statement inside for testing
break;
}
}
}

以及我的 R 文件的摘录:

public static final class id {
public static final int action_settings=0x7f08000e;
public static final int finalize_entry_button=0x7f08000d;
public static final int separator=0x7f080002;
public static final int time_frag_current_time=0x7f080003;
public static final int time_frag_date_string=0x7f080001;
public static final int time_frag_in_label=0x7f08000b;
public static final int time_frag_in_picker=0x7f08000c;
public static final int time_frag_mark_button=0x7f080004;
}

根据要求,初始化监听器的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myClickListener = new MyClickListener();
myTimeFilter = new IntentFilter(Intent.ACTION_TIME_TICK);
myTimeTicker = new MyTimeTicker();
getApplicationContext().registerReceiver(myTimeTicker, myTimeFilter);
systemTime = (TextView) findViewById(R.id.time_frag_current_time);
systemTime.setText(sdf.format(new Date()));
systemDate = (TextView) findViewById(R.id.time_frag_date_string);
outTimeView = (TextView) findViewById(R.id.time_frag_out_picker);
outTimeView.setOnClickListener(myClickListener);
offTimeView = (TextView) findViewById(R.id.time_frag_off_picker);
offTimeView.setOnClickListener(myClickListener);
onTimeView = (TextView) findViewById(R.id.time_frag_on_picker);
onTimeView.setOnClickListener(myClickListener);
inTimeView = (TextView) findViewById(R.id.time_frag_in_picker);
inTimeView.setOnClickListener(myClickListener);
markButton = (Button) findViewById(R.id.time_frag_mark_button);
markButton.setOnClickListener(myClickListener);
//should error check this, only open a new one if one is not in progress
//need to start saving the Entry to disk during onPause() and move this check to onStart() or something
currentEntry = new LogEntry();
}

最佳答案

您是否尝试过查看按下 R.id.finalize_entry_button 时输出的 ID 是否符合您的预期?

另外,您可以发布将 OnClick 处理程序连接到您的控件的代码吗?

关于java - Android onClick(View) 与 switch 不正确的情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20081194/

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