2014-07-06 84 views
0

我有一個由四個imageViews組成的自定義首選項。我想在點擊4個imageViews中的第4個時開始一個活動。開始活動自定義首選項android

public class ImagePreference4Locks extends Preference { 
public ImagePreference4Locks(final Context context, final AttributeSet attrs, 
      final int defStyle) { 
     super(context, attrs, defStyle); 
     this.setLayoutResource(R.layout.imagepref4locks); 
} 

@Override 
     protected void onBindView(View view) {final Context m = this.getContext(); 
     super.onBindView(view); 

     final ImageView thumb_1 = (ImageView) view.findViewById(R.id.thumb_1); 
     final ImageView thumb_2 = (ImageView) view.findViewById(R.id.thumb_2); 
     final ImageView thumb_3 = (ImageView) view.findViewById(R.id.thumb_3); 
     final ImageView thumb_4 = (ImageView) view.findViewById(R.id.thumb_4); 
     ......... 

if (thumb_4 != null) { 

      thumb_4.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
      ///////////////////////////////////////////// 
      Start Activity here: 
}} 

我怎樣才能從這裏開始的活動,因爲我無法獲得上下文.......

使用這個難道不工作:

Intent cc=new Intent(this.getContext(),HomeActivity.class); 
this.getContext().startActivity(cc); 

回答

1

在你被迫到constuctor通過上下文,爲什麼你不創建一個私人變量,如

private static Context mContext; 

然後你在那裏初始化它?

public ImagePreference4Locks(final Context context, final AttributeSet attrs, 
     final int defStyle) { 
    super(context, attrs, defStyle); 
    mContext = context; 
    this.setLayoutResource(R.layout.imagepref4locks); 
} 

下面這段代碼應該工作

Intent cc = new Intent(mContext, HomeActivity.class); 
mContext.startActivity(cc);