1

我正在使用一种方法: 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();
        }
4

4 回答 4

5

传递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;
}

编辑:错字

于 2013-05-17T14:21:11.080 回答
4

您单击的视图是onClick. 如果你需要它的 id 你可以通过

int id = view.getId();

这样你就可以打开 R.id.yourButtonId 来了解哪个被点击了

于 2013-05-17T14:19:56.420 回答
2

你很幸运 -View view传递给你的处理程序的是被点击的实际视图。所以视图本身就是按钮。查看文档

于 2013-05-17T14:14:16.683 回答
0

在此方法中,您已通过视图哪个触发的事件(Button)v应该为您提供参考

public void onClick(View v)
于 2013-05-17T14:15:26.917 回答