gpt4 book ai didi

android - 如何知道哪个按钮调用了方法?

转载 作者:行者123 更新时间:2023-11-29 14:23:16 24 4
gpt4 key购买 nike

我正在使用一种方法:button_click(View view) 在 editText 上设置文本,我有很多按钮,每个按钮都应该在特定的 editText 上设置文本。

有什么方法可以知道哪个按钮调用了该方法,以便我可以设置正确的 editText 的文本?

方法代码如下:

public void button_click(View view) {

// Create the dialog
final Dialog mDateTimeDialog = new Dialog(view.getContext());
// Inflate the root layout
LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
final RelativeLayout mDateTimeDialogView = (RelativeLayout) inflater.inflate(R.layout.datepick, null);
// Grab widget instance
final DateTimePicker mDateTimePicker = (DateTimePicker) mDateTimeDialogView
.findViewById(R.id.DateTimePicker);
mDateTimePicker.setDateChangedListener(this);

// Update demo edittext when the "OK" button is clicked
((Button) mDateTimeDialogView.findViewById(R.id.SetDateTime))
.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mDateTimePicker.clearFocus();
// TODO Auto-generated method stub
String result_string = mDateTimePicker.getMonth()
+ "/"
+ String.valueOf(mDateTimePicker.getDay())
+ "/"
+ String.valueOf(mDateTimePicker.getYear())
+ " "
+ String.valueOf(mDateTimePicker.getHour())
+ ":"
+ String.valueOf(mDateTimePicker.getMinute());

edit_text1.setText(result_string);
mDateTimeDialog.dismiss();
}
});

// Cancel the dialog when the "Cancel" button is clicked
((Button) mDateTimeDialogView.findViewById(R.id.CancelDialog))
.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mDateTimeDialog.cancel();
}
});

// Reset Date and Time pickers when the "Reset" button is clicked

((Button) mDateTimeDialogView.findViewById(R.id.ResetDateTime))
.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
mDateTimePicker.reset();
}
});

// Setup TimePicker
// No title on the dialog window
mDateTimeDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Set the dialog content view
mDateTimeDialog.setContentView(mDateTimeDialogView);
// Display the dialog
mDateTimeDialog.show();
}

最佳答案

传递的View其实就是按钮。您需要做的就是像这样切换 ID:

switch (view.getId()) {
case R.id.button1:
// Do something here related to button 1
break;
case R.id.button2:
// Do something here related to button 2
break;
}

编辑:打字错误

关于android - 如何知道哪个按钮调用了方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16611198/

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