作业帮 > 综合 > 作业

android this的用法~麻烦跟我解释一下这里面的几个this~

来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/11 07:26:33
android this的用法~麻烦跟我解释一下这里面的几个this~
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License,Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing,software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis.view;
import com.example.android.apis.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
/**
* Uses a TextSwitcher.
*/
public class TextSwitcher1 extends Activity implements ViewSwitcher.ViewFactory,
View.OnClickListener {
private TextSwitcher mSwitcher;
private int mCounter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_switcher_1);
mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
mSwitcher.setInAnimation(in);
mSwitcher.setOutAnimation(out);
Button nextButton = (Button) findViewById(R.id.next);
nextButton.setOnClickListener(this);
updateCounter();
}
public void onClick(View v) {
mCounter++;
updateCounter();
}
private void updateCounter() {
mSwitcher.setText(String.valueOf(mCounter));
}
public View makeView() {
TextView t = new TextView(this);
t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
t.setTextSize(36);
return t;
}
}
android this的用法~麻烦跟我解释一下这里面的几个this~
this 当前类(activity等)其指向是当前的.
比如程序中的nextButton.setOnClickListener(this);
这个this 就相当当前类的Context,即为,当期
如下,
nextButton.setOnClickListener(new OnClickListener() {
\x05\x05
\x05\x05@Override
\x05\x05public void onClick(View v) {
Toast.makeText(TextSwitcher1.this,"看这里",5000).show();
\x05\x05\x05}
在这个里面Toast(context,string,time),中,你直接写this 会出错的,因为你当前 的类是OnClickListener类.而不失我们的Activity 类,这样你写上去的会出现错误的!所以在内部类中你写this,指的是内部类,而不是外部的!this 就是当前,TextSwitcher1.this后面能调出TextSwitcher1的所有属性!
另外咱们在调用一些方法,传的Context 就是把当前类 传过去使用.
\x05\x05\x05
\x05\x05}
\x05};)