如何在另一个方法中使用findViewById变量。 Java Android

时间:2012-02-10 13:17:47

标签: java android variables switch-statement findviewbyid

我对编程非常陌生并且正在自学,如果我的代码很丑,那就很抱歉。

我正在尝试创建一个菜单,如屏幕,左右箭头按钮可以更改显示的图片。我想这样做,当你在第一张图像上左箭头消失时,当显示最后一张图像时,右箭头消失了。

我尝试过很多不同的方法,但总是需要一个无法解决的变量。在发布的代码中,错误出现在switch语句中,而我试图对其进行编码,其他未解决的变量错误将在onClickListener中。

你不必为我编码,只是让我知道我缺少什么概念,我会研究它。

由于

    package com.mystuff.mymenu;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ImageButton;
    import android.widget.ImageView;



public class PictureMenu extends Activity implements OnClickListener{

int setView = 1;    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.picture_menu);  

    menuSet(setView);

ImageButton right = (ImageButton) findViewById(R.id.rightButton);
ImageButton left = (ImageButton) findViewById(R.id.leftButton);
right.setOnClickListener(this);
left.setOnClickListener(this);

}       
@Override
public void onClick(View v) {
if(v.getId() == R.id.rightButton){
    setView ++;
    menuSet(setView);
if(v.getId() == R.id.rightButton){
    setView ++;
        menuSet(setView);
}
    }   
    }


private void menuSet(int setView) {

    ImageView picture1 = (ImageView) findViewById(R.id.picture1);
    ImageView picture2 = (ImageView) findViewById(R.id.picture2);
    ImageView picture3 = (ImageView) findViewById(R.id.picture3);
    ImageView picture4 = (ImageView) findViewById(R.id.picture4);

    switch(setView){
    case 1: 
        left.setVisibility(View.GONE);  //ERROR left cannot be resolved
        right.setVisibility(View.VISIBLE); //ERROR right cannot be resolved
        picture1.setVisibility(View.VISIBLE);
        picture2.setVisibility(View.GONE);
        picture3.setVisibility(View.GONE);
        picture4.setVisibility(View.GONE);
        break;
    case 2:
        left.setVisibility(View.VISIBLE);  //ERROR left cannot be resolved
        right.setVisibility(View.VISIBLE);  //ERROR right cannot be resolved
        picture1.setVisibility(View.GONE);
        picture2.setVisibility(View.VISIBLE);
        picture3.setVisibility(View.GONE);
        picture4.setVisibility(View.GONE);
        break;
    case 3: 
        left.setVisibility(View.VISIBLE);  //ERROR left cannot be resolved
        right.setVisibility(View.VISIBLE);  //ERROR right cannot be resolved
        picture1.setVisibility(View.GONE);
        picture2.setVisibility(View.GONE);
        picture3.setVisibility(View.VISIBLE);
        picture4.setVisibility(View.GONE);
        break;
    case 4: 
        left.setVisibility(View.VISIBLE); //ERROR left cannot be resolved
        right.setVisibility(View.GONE); //ERROR right cannot be resolved
        picture1.setVisibility(View.GONE);
        picture2.setVisibility(View.GONE);
        picture3.setVisibility(View.GONE);
        picture4.setVisibility(View.VISIBLE);
        break;
    default:
        left.setVisibility(View.GONE);  //ERROR left cannot be resolved
        right.setVisibility(View.VISIBLE);  //ERROR right cannot be resolved
        picture1.setVisibility(View.VISIBLE);
        picture2.setVisibility(View.GONE);
        picture3.setVisibility(View.GONE);
        picture4.setVisibility(View.GONE);
        break;
}       


}



}

2 个答案:

答案 0 :(得分:5)

您的问题在于正确 onCreate()方法的本地方法,并且无法从该方法外部看到。如果你想要它们的类范围,可以使它们成为类属性,例如:

public class PictureMenu extends Activity implements OnClickListener {

    int setView = 1; 
    ImageButton right, left;
    ...

    protected void onCreate(Bundle savedInstanceState) {
        ...
        right = (ImageButton) findViewById(R.id.rightButton);
        left = (ImageButton) findViewById(R.id.leftButton);
        ...
    }

另一种解决方案是将它们作为参数传递给 menuSet()

恕我直言,你应该首先学习正确的编程,特别是variable scope。 正确格式化代码有助于轻松发现范围。

答案 1 :(得分:1)

这应该有效:

    package com.mystuff.mymenu;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ImageButton;
    import android.widget.ImageView;



public class PictureMenu extends Activity implements OnClickListener{

private ImageButton right, left; 

int setView = 1;    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.picture_menu);  

    menuSet(setView);

right = (ImageButton) findViewById(R.id.rightButton);
left = (ImageButton) findViewById(R.id.leftButton);
right.setOnClickListener(this);
left.setOnClickListener(this);

}       
@Override
public void onClick(View v) {
if(v.getId() == R.id.rightButton){
    setView ++;
    menuSet(setView);
if(v.getId() == R.id.rightButton){
    setView ++;
        menuSet(setView);
}
    }   
    }


private void menuSet(int setView) {

    switch(setView){
    case 1: 
        left.setVisibility(View.GONE);  //ERROR left cannot be resolved
        right.setVisibility(View.VISIBLE); //ERROR right cannot be resolved
        picture1.setVisibility(View.VISIBLE);
        picture2.setVisibility(View.GONE);
        picture3.setVisibility(View.GONE);
        picture4.setVisibility(View.GONE);
        break;
    case 2:
        left.setVisibility(View.VISIBLE);  //ERROR left cannot be resolved
        right.setVisibility(View.VISIBLE);  //ERROR right cannot be resolved
        picture1.setVisibility(View.GONE);
        picture2.setVisibility(View.VISIBLE);
        picture3.setVisibility(View.GONE);
        picture4.setVisibility(View.GONE);
        break;
    case 3: 
        left.setVisibility(View.VISIBLE);  //ERROR left cannot be resolved
        right.setVisibility(View.VISIBLE);  //ERROR right cannot be resolved
        picture1.setVisibility(View.GONE);
        picture2.setVisibility(View.GONE);
        picture3.setVisibility(View.VISIBLE);
        picture4.setVisibility(View.GONE);
        break;
    case 4: 
        left.setVisibility(View.VISIBLE); //ERROR left cannot be resolved
        right.setVisibility(View.GONE); //ERROR right cannot be resolved
        picture1.setVisibility(View.GONE);
        picture2.setVisibility(View.GONE);
        picture3.setVisibility(View.GONE);
        picture4.setVisibility(View.VISIBLE);
        break;
    default:
        left.setVisibility(View.GONE);  //ERROR left cannot be resolved
        right.setVisibility(View.VISIBLE);  //ERROR right cannot be resolved
        picture1.setVisibility(View.VISIBLE);
        picture2.setVisibility(View.GONE);
        picture3.setVisibility(View.GONE);
        picture4.setVisibility(View.GONE);
        break;
}       


}



}