简体   繁体   中英

Having trouble understanding this line of code

If anyone wants to break down this code and explain it to me. I'd be thankful.

I run into an error on view(cannot resolve symbol), not sure if I'm supposed to replace that with a specific view ?

Btw, this is an onClick method.

"else if(view.getId()==R.id.Button9){}"

What I understand from this code is it says when "if" "view" whatever the viewid is goes in here ()?? == <--is related to R.id.button9 then run this block of code. Am i even close? Thanks.

A little backstory, I have created an ImageButton and when it's clicked, I'd like to to clear the screen. I've built on onclicklistener and implemented view.OnClickListener on my user public class.

CLEARCANVAS = (ImageButton) findViewById(R.id.button9);
CLEARCANVAS.setOnClickListener(this);

@Override
public void onClick(View v) {
    if (view.getId()==R.id.button9);
}

Your View parameter is v , not view .

Change it to v and it will compile:

@Override
public void onClick(View v) {
   //if (view.getId()==R.id.button9){
   if (v.getId() == R.id.button9){
      //handle button9 click
   }
}

Another common way to do it is to define a separate click listener for each clickable element, for example:

    clearCanvas = (ImageButton) findViewById(R.id.button9);
    clearCanvas.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Handle button9 click
        }
    });

If the error is related with class R, then you need to rebuild the project or else if its still not working for you, you will have to restart the IDE you are using. Basically if you are using Eclipse, I suggest to change to Android Studio as google has deprecated android support to eclipse. Cannot resolve symbol error basically means that the project does not have particular class reference or whatever you are trying to use.

It is like this.

You have 10 buttons from button1 to button10 . Everything has an id stored in R.java file to uniquely identify it.

Suppose you want to perform a onClick event for button9 . You create a generic onClick event for all the buttons.

@Override
public void onClick(View v) {
//now all the 10 buttons have the same onclick event.
}

Now to differentiate between which button was clicked, you need to use it's id.

@Override
public void onClick(View v) {
if (v.getId()==R.id.button9){
  Toast.makeText(context, "Button 9 Clicked!", Toast.LENGTH_SHORT).show();
}
if (v.getId()==R.id.button8){
  Toast.makeText(context, "Button 8 Clicked!", Toast.LENGTH_SHORT).show();
}
...
}

So, R.java is a link between your UI objects in the XML and the java code.

You'll need the R.java to identify each object from the UI.

This is automatically done. If you get an error saying cannot resolve R . You might want to re-build or clean your project .

you just need to change your view.getId() with the v.getId().

The view which is passed in the OnClick method is the view which is clicked, so It will check which Id is clicked.

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