2013-06-03 27 views
0

我正在使用this问题的代码(第一个答案)在一个对话框中选择日期和时间。不过,我正在做一个开始时间和结束时间,所以有两个按钮可以打开同一个对话框界面。有没有办法找出哪一个打开的对话框界面,使我的onSet方法我可以这样做:有没有办法告诉哪个按钮打开了一个对话框界面?

 

    if (start_button opened the dialog) { 
     return result to start time TextView; 
    } else if (end_button opened the dialog) { 
     return result to end time TextView; 
    } 

+0

怎么样一个布尔标志设置为true无论选择哪个按钮,您都可以在条件中检查该标志以确定选择了哪个按钮。当然必要时保持国旗。 – Ryan

回答

1

可以使用android:[email protected]+id/buttonX值制定出按下了哪个按钮。在您的活动代码

像这样的东西(也许):

private int mButtonPressed = -1; 

... heap of code ... 

public void pressedButton(View view) { 
    mButtonPressed = view.getId(); 
} 

// your code from your question 
if (mButtonPressed == R.id.buttonX) { 
    return result to start time TextView; 
} else if (mButtonPressed == R.id.buttonY) { 
    return result to end time TextView; 
} 

而且在布局XML的按钮,请确保您有:

<Button 
    android:id="@+id/buttonX" 
    android:onclick="pressedButton" 
    ... more attributes ... 
/> 
<Button 
    android:id="@+id/buttonY" 
    android:onclick="pressedButton" 
    ... more attributes ... 
/> 
相关问题