代码之家  ›  专栏  ›  技术社区  ›  Haroldas Poderskis

无法解析Android Studio 3.0 Canary 8中的符号“Value”

  •  0
  • Haroldas Poderskis  · 技术社区  · 7 年前

    无法解析符号“Value”

    enter image description here

    以下是主要活动。java代码:

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
    private Button btnAdd;
    private Button btnTake;
    private TextView txtValue;
    private Button btnGrow;
    private Button btnShrink;
    private Button btnReset;
    private Button btnHide;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // get reference to all buttons in UI. Match them to all declared Button objects
        btnAdd = (Button) findViewById(R.id.btnAdd);
        btnTake = (Button) findViewById(R.id.btnTake);
        txtValue = (TextView) findViewById(R.id.txtValue);
        btnGrow = (Button) findViewById(R.id.btnGrow);
        btnShrink = (Button) findViewById(R.id.btnShrink);
        btnReset = (Button) findViewById(R.id.btnReset);
        btnHide = (Button) findViewById(R.id.btnHide);
    
        // listen for all the button clicks
        btnAdd.setOnClickListener(this);
        btnTake.setOnClickListener(this);
        txtValue.setOnClickListener(this);
        btnGrow.setOnClickListener(this);
        btnShrink.setOnClickListener(this);
        btnReset.setOnClickListener(this);
        btnHide.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View view) {
    
        // a local variable to use later
        float size;
    
        switch (view.getId()){
    
            // case 1
            case  R.id.btnAdd:
                value++;
                txtValue.setText(""+ value);
    
                break;
    
            // case 2
            case  R.id.btnTake:
                value--;
                txtValue.setText(""+ value);
    
                break;
    
            // case 3
            case R.id.btnReset:
                value = 0;
                txtValue.setText(""+ value);
    
                break;
    
            // case 4
            case R.id.btnGrow:
                size = txtValue.getTextScaleX();
                txtValue.setTextScaleX(size + 1);
    
                break;
    
            // case 5
            case R.id.btnShrink:
                size = txtValue.getTextScaleX();
                txtValue.setTextScaleX(size - 1);
    
                break;
    
            // last case statement with if-else
            case R.id.btnHide:
                if (txtValue.getVisibility() == View.VISIBLE){
    
                    // currently visible so hide it
                    txtValue.setVisibility(View.INVISIBLE);
    
                    // change text on the button
                    btnHide.setText("SHOW");
                }else{
                    // hidden so show
                    txtValue.setVisibility(View.VISIBLE);
    
                    // change text on button
                    btnHide.setText("HIDE");
                }
    
                break;
    
        }
    
    
    }}
    

    如果您能快速查看一下语法是否有错误,我将非常感激。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Kapil G    7 年前

    如下所示声明值变量-

    private int value;
    

    在你的onCreate方法之上。变量声明是必需的,然后才能在代码中的任何位置使用它。如果将其设置为全局变量,则可以从onClick方法访问它,也可以从外部的任何其他地方访问它。