简体   繁体   English

无法理解这一行代码

[英]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. 顺便说一句,这是一个onClick方法。

"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 ()?? 我从这段代码中了解到的是,当“ if”“ view”(无论viewid是什么)进入此处时,它表示什么? == <--is related to R.id.button9 then run this block of code. == <-与R.id.button9有关,然后运行此代码块。 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. 有点背景,我创建了一个ImageButton ,当单击它时,我想清除屏幕。 I've built on onclicklistener and implemented view.OnClickListener on my user public class. 我建立在onclicklistener并在我的用户公共类上实现了view.OnClickListener

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 . 您的View参数是v ,不是view

Change it to v and it will compile: 将其更改为v ,它将编译:

@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. 如果错误与类R有关,则您需要重建项目,否则,如果项目仍不起作用,则必须重新启动使用的IDE。 Basically if you are using Eclipse, I suggest to change to Android Studio as google has deprecated android support to eclipse. 基本上,如果您使用的是Eclipse,我建议更改为Android Studio,因为google已弃用android支持。 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 . button1button10您有10个按钮。 Everything has an id stored in R.java file to uniquely identify it. 一切都有一个ID存储在R.java文件中以唯一标识它。

Suppose you want to perform a onClick event for button9 . 假设您要为button9执行一个onClick事件。 You create a generic onClick event for all the buttons. 您为所有按钮创建一个通用的onClick事件。

@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. 现在要区分单击哪个按钮,您需要使用它的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. 因此,R.java是XML中的UI对象与Java代码之间的链接。

You'll need the R.java to identify each object from the UI. 您将需要R.java从UI识别每个对象。

This is automatically done. 这是自动完成的。 If you get an error saying cannot resolve R . 如果出现错误,则说无法解决R You might want to re-build or clean your project . 您可能想要重新构建或清理您的项目

you just need to change your view.getId() with the v.getId(). 您只需要使用v.getId()更改view.getId()。

The view which is passed in the OnClick method is the view which is clicked, so It will check which Id is clicked. 在OnClick方法中传递的视图是被单击的视图 ,因此它将检查被单击的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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