0

我创建了一个 datetime 组件,但它是自动构建的(我在 XML 布局中拥有它,我不想手动创建它)但我需要传递对 an 的引用Activity才能创建对话框。我怎样才能做到这一点?之后我尝试了一个二传手,findViewById但这不是一个好的解决方案......

public class DateTimeComponent extends RelativeLayout {

    private Activity activity;

    public DateComponent(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        // rest ommited
        initFields();
    }

    private void initFields() {
        dateEditText = (EditText) findViewById(R.id.dateEditText);
        dateEditText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.showDialog(DATE_PICKER_DIALOG);
            }
        });

        timeEditText = (EditText) findViewById(R.id.timeEditText);
        timeEditText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.showDialog(TIME_PICKER_DIALOG);
            }
        });
    }

    // rest ommited

    public Dialog getDatePickerDialog() {
        int year = selectedDateTime.get(YEAR);
        int month = selectedDateTime.get(MONTH);
        int day = selectedDateTime.get(DAY_OF_MONTH);
        return new DatePickerDialog(activity, onDateSetListener, year, month, day);
    }

    public Dialog getTimePickerDialog() {
        int hour = selectedDateTime.get(HOUR_OF_DAY);
        int minute = selectedDateTime.get(MINUTE);
        return new TimePickerDialog(activity, onTimeSetListener, hour, minute, true);
    }

    private final OnDateSetListener onDateSetListener = new OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            // do something
        }
    };

    private final OnTimeSetListener onTimeSetListener = new OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            // do something
        }
    };

}
4

3 回答 3

1

您的构造函数接收的上下文是一个活动。因此,您可以将其投射到它上面。例如像这样

MyActivity a = (MyActivity) getContext();

PS您不需要在自己的字段中存储活动:

private Activity activity; // not needed

里面已经存储了,可以通过http://developer.android.com/reference/android/view/View.html#getContext ()获取

证明

自定义文本视图:

public class MyTextView extends TextView {

public MyTextView(Context context) {
    super(context);
    setText(Integer.toString(System.identityHashCode(context)));
}

public MyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setText(Integer.toString(System.identityHashCode(context)));
}


}

活动:

public class ContextActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView textView = (TextView) findViewById(R.id.textView);

    textView.setText( Integer.toString(System.identityHashCode(this)) );
}
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView" />

<com.inthemoon.incubation.MyTextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

</LinearLayout>

显示的代码相同。

于 2012-05-07T09:24:16.313 回答
1

也许这可以帮助你:

选项1:

public class DateTimeComponent extends RelativeLayout {
    private Activity activity;

    public DateTimeComponent(Activity act){
        activity = act;
    }

    public void someListener() {
        activity.showDialog(...);
    }

}

选项 2:

public class DateTimeComponent extends RelativeLayout {

    public void someListener(Activity act) {
        act.showDialog(...);
    }

}

选项 3:

...
    private Activity activity;

    public DateComponent(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);

        activity = (Activity) getContext();

        // rest ommited
        initFields();
    }

...
于 2012-05-07T09:07:58.403 回答
1

两种方式——

  1. 创建一个接受 Context 参数的构造函数,并拥有一个 Context 类型的(私有?)类变量,您可以随时使用它。

  2. 为每个需要它的方法添加一个额外的 Context 上下文参数。在某些情况下,您可能需要进行最终处理。

于 2012-05-07T09:16:59.420 回答