public class ShuDuKuActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //为每个按钮设置点击监听
        View continueButton = this.findViewById(R.id.button_continue);
        continueButton.setOnClickListener((OnClickListener) this);
        View newButton = this.findViewById(R.id.button_new_game);
        newButton.setOnClickListener((OnClickListener) this);
        View aboutButton = this.findViewById(R.id.button_about);
        aboutButton.setOnClickListener((OnClickListener) this);
        View exitButton = this.findViewById(R.id.button_exit);
        exitButton.setOnClickListener((OnClickListener) this);
    }里面的View continueButton = this.findViewById(R.id.button_continue); this指针指哪个对象?
continueButton.setOnClickListener((OnClickListener) this);这里的this指针又是指哪个对象?跟上面一样的吗?
新手求学习,谢谢各位

解决方案 »

  1.   

    this就是指当前ShuDuKuActivity类的对象实例,两个this都是指同一个对象.
      

  2.   

    continueButton.setOnClickListener((OnClickListener) this);
    怎么感觉这个this指针可以随便强制转换的?
      

  3.   

    continueButton.setOnClickListener((OnClickListener) this);这个是接口回调因为ShuDuKuActivity 实现了OnClickListener接口,重写了OnClick方法,这样通过强制转换为该接口类型就可以调用重写后的方法,也就是多态(Polymorphism).
      

  4.   

    貌似明了,又貌似不太明,我想再请教下下面的,也是在同一个类里private void CreateDifficultDialog()
    {
    Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.new_game_title);
    builder.setItems(R.array.difficulty,
    new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
    startGame(which);
    }
    })
    .show();
    }

    private void startGame(int i){
    Log.d(TAG, "clicked on" + i);
    }为什么这里面的Builder builder = new AlertDialog.Builder(this); this指针又不用强制转换呢?
      

  5.   

    两个this都是指类的当前实例,后面的强制转换其实也没有必要,本身该类就实现了OnClickListener,所以可以将其作为接口对象来使用。
      

  6.   


    本人混乱了,明明这个 AlertDialog.Builder()的参数是Context context 为什么可以用THIS指针啊
      

  7.   

    java.lang.Object
       ↳ android.content.Context
         ↳ android.content.ContextWrapper
           ↳ android.view.ContextThemeWrapper
             ↳ android.app.Activity