简体   繁体   中英

Quick 'this' Keyword Query - Help Appreicated

So essentially having trouble understanding the uses of the 'this' keyword in java. Have read around five tutorials, albeit not very good ones. Could someone quickly explain how it's used in relation to the following code? This is an android, it's assigning an xml button (btn_confirm) to b with Button type.

   Button b = (Button)this.findViewById(R.id.btn_confirm);

b.setonclickListener(this);

Full Code:

public class dic_tut2 extends Activity implements onclickListener {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button b = (Button)this.findViewById(R.id.btn_confirm);
    b.setonclickListener(this);
}

public void onclick(View v) {

    TextView tv = (TextView)this.findViewById(R.id.tv_welcome);
    EditText et = (EditText)this.findViewById(R.id.txt_name);

    String text = "Hello, " + et.getText().toString() + ".\n\n";
    text += "Welcome to android development. :)";

    tv.setText(text);

}

}

this refers to the current instance of the class where you are using it in. For example:

class A {
    public void f() {
       A obj = this; // current instance of A
    }
}

So if later I create an instance of A:

public static void main(String[] args) {

    A a = new A();
    a.f(); // the 'this' inside f() is actually 'a'.
}

So your code:

b.setonclickListener(this);

Sets the onclickListener of the button b to the current instance (the instance from which you called the method) of the class you are in.

Edit : Ok here is a complete example:

class A {
   public char c;
   public A(char c) { this.c = c; }
   public void f() {
       System.out.println(this.c);
   }
}

public static void main(String[] args) {
   A a = new A('a');
   a.f(); // prints 'a' because 'this' inside f() is actually object 'a'.

   A b = new A('b');
   b.f(); // prints 'b' because 'this' inside f() is actually object 'b'.      
}

In the examples you have seen, the class is most likely an activity class. Something like this:

class MainActivity extends Activity implements OnClickListener {

    @Override
    protected onCreate(Bundle bundle) {
        Button b = (Button)this.findViewById(R.id.btn_confirm);

        b.setOnClickListener(this);

    } 

    @Override
    public onClick(View v) {
        // Do something!
    }
}

This is possible because the object "this" is both an activity which can have a button in its View and it also implements OnClickListener which allows its onClick method to be called by that button when it is clicked. This is done by providing a reference to itself to the setOnClickListener function of the button.

b.setonclickListener(this);

This means the click listener for b is this : the current object. In this case, it's your Activity .

The Activity implements OnClickListener , which provides the onclick method.

You're basically saying "Button, when you're clicked, here's how to handle it--you handle it with the implementation contained within myself."

It wouldn't have to be this , it could be anything that implemented OnClickListener . By keeping the implementation local to the Activity you provide easy access to its (the Activity 's) member variables (which you don't need in this case).

Inside the onclick handler the this references are superfluous--you can access the findViewById methods without the this . Member methods and variables are accessible without the qualifier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM