2012-05-07 3 views
0

日時コンポーネントを作成しましたが、自動的に構築されました(私はそれをXMLレイアウトで作成し、手動で作成する必要はありません)。ダイアログを作成するには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 
     } 
    }; 

} 
+0

Activity/Contextの参照をコンストラクタ経由でDateTimeComponentに渡すことができます。 – adatapost

+0

この目的のためにコンテキストを使用することができます – Akram

+0

@Akkiコンテキストには 'showDialog()'メソッドがありません.. – user219882

答えて

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(); 
    } 

... 
+0

コンポーネントを作成しません、 'DateComponent component = (DateComponent)findViewById(R.id.dateComponent) ' – user219882

+0

私の更新された回答を参照 – waqaslam

+0

私の質問が更新されました...コンポーネント自体のすべてのリスナーを初期化するため、どちらのオプションも使用できません。 – user219882

1

二つの方法 -

  1. コンテキストパラメータを受け取るコンストラクタを作成し、あなたがいつでも使用できるContext型の(プライベート?)クラス変数を持っています。

  2. 必要となるすべてのメソッドに対して追加のコンテキストコンテキストパラメータを追加します。場合によっては、その最終決定を下す必要があるかもしれません。

1

コンストラクタが受け取るコンテキストはアクティビティです。それで、あなたはそれをそれにキャストすることができます。例えば、次のようになります。

​​

P.S.それはすでに内部に格納され、http://developer.android.com/reference/android/view/View.html#getContext()

PROOF

カスタムテキストビューによって得ることができる

private Activity activity; // not needed 

あなたはあなた自身の分野での活動を保管する必要はありません。

アクティビティ:

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))); 
} 
} 

レイアウト:diplayed

<?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> 

符号は同一です。

+0

私は興味があります - 'あなたのコンストラクタが受け取るコンテキストはアクティビティです。 – MByD

+0

'Activity'クラスは' Context'の子孫です。私は実験的に決定しました。そのアクティビティは、説明されているような単純なケースでコンストラクタに渡されます。私は初心者であり、常にそうであることを保証することはできません。 'Context'は複数の実装を持っているので、ある場合にはそれらを使うことができます。 –

+0

これは証明ではありません。 **現在の**プラットフォーム上の**現在の**実装であることを示しています。 – MByD

関連する問題