I am very new to programming and am teaching myself so sorry if my code is ugly.
我对编程非常陌生并且正在教自己,如果我的代码很丑,那就很抱歉。
I am trying to create a menu like screen that has left and right arrow buttons that change the picture displayed. I want to make it so when your on the first image the left arrow is gone and when on the last image is displayed the right arrow is gone.
我正在尝试创建一个菜单,如屏幕,左右箭头按钮可以更改显示的图片。我想这样做,当你在第一张图像上左箭头消失时,当显示最后一张图像时,右箭头消失了。
I have tried to do this a lot of different ways but always end up needing a variable somewhere where it cannot be resolved. In the code posted the error is in the switch statement and I other ways I have tried to code it the unresolved variable error would be in the onClickListener.
我试图以很多不同的方式做到这一点,但总是需要一个无法解决的变量。在发布的代码中,错误在switch语句中,而我尝试对其进行编码的其他方法是未解决的变量错误将在onClickListener中。
You don't have to code it for me, just let me know what concept I am missing and I will study up on it.
你不必为我编码,只是让我知道我缺少什么概念,我会研究它。
Thanks
谢谢
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 个解决方案
#1
5
Your problem lies in that right and left are local to onCreate() method, and cannot be seen from outside that method. If you want them class-scope, make them class attributes, e.g.:
你的问题在于,右边和左边是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);
...
}
Another solution would be passing them as arguments to menuSet()
另一种解决方案是将它们作为参数传递给menuSet()
IMHO, you should first learn proper programming, specially what variable scope is. Also properly formatting your code helps easily spotting scopes.
恕我直言,你应该首先学习正确的编程,特别是变量范围。正确格式化代码有助于轻松发现范围。
#2
1
This should work:
这应该工作:
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;
}
}
}
#1
5
Your problem lies in that right and left are local to onCreate() method, and cannot be seen from outside that method. If you want them class-scope, make them class attributes, e.g.:
你的问题在于,右边和左边是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);
...
}
Another solution would be passing them as arguments to menuSet()
另一种解决方案是将它们作为参数传递给menuSet()
IMHO, you should first learn proper programming, specially what variable scope is. Also properly formatting your code helps easily spotting scopes.
恕我直言,你应该首先学习正确的编程,特别是变量范围。正确格式化代码有助于轻松发现范围。
#2
1
This should work:
这应该工作:
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;
}
}
}