Android onClick(查看)与开关不正确的情况

时间:2013-11-19 20:17:55

标签: java android onclicklistener

我有一个OnClickListener实例可以将点击引导到多个其他处理程序方法,通常是:

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

当我点击视图A时,会调用handleButtonA()。当我单击视图B时,没有任何反应。当我点击任何其他视图时,它会调用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();
}

1 个答案:

答案 0 :(得分:2)

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

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