我正在尝试实现一个按钮单击事件以在图像切换器中切换图像以及在按钮单击时更新 textView 字符串。到目前为止,我已经让它适用于图像,但是当我尝试修改方法以包括更新我收到以下错误:
在 getMyString 调用中,我得到以下信息: AboutActivity 类型中的方法 getMyString(int, Context) 不适用于参数 (int)
在 getMyString 的方法头 this: Multiple markers at this line
- 令牌“(”,;预期的语法错误
- 标记 ")" 的语法错误,;预期的
我在开关中调用字符串的地方是:Void methods cannot return a value
- 参数 getMyString 的非法修饰符;只允许final
有谁知道我在这个实现中哪里出错了?我认为它可能缺少大括号或者代码被置于上下文之外,但从查看错误来看,这似乎是我调用的方式onClick 事件中的 getMyString()。
public class AboutActivity extends Activity implements OnClickListener{
private ImageSwitcher imageSwitcher;
Button btnNext;
TextView tView;
int imageIds[]=
{R.drawable.mark1,R.drawable.mark2,R.drawable.mark3};
int messageCount=imageIds.length;
// to keep current Index of ImageID array
int currentIndex=-1;
private int clicks = 1;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
final Intent intent1=new Intent(this,AboutActivity.class);
final Intent intent2=new Intent(this,MainActivity.class);
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.a,null);
// get The references
btnNext=(Button)findViewById(R.id.buttonNext);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
tView = (TextView) findViewById(R.id.textView1);
// Set the ViewFactory of the ImageSwitcher that will create ImageView object when asked
imageSwitcher.setFactory(new ViewFactory() {
public View makeView() {
// TODO Auto-generated method stub
// Create a new ImageView set it's properties
ImageView imageView = new ImageView(getApplicationContext());
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new
ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
return imageView;
}
});
// Declare the animations and initialize them
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);
// set the animation type to imageSwitcher
imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);
// ClickListener for NEXT button
// When clicked on Button ImageSwitcher will switch between Images
// The current Image will go OUT and next Image will come in with specified animation
btnNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
currentIndex++;
// If index reaches maximum reset it
if(currentIndex==messageCount)
currentIndex=0;
imageSwitcher.setImageResource(imageIds[currentIndex]);
tView.setText(getMyString(clicks++, v.getContext()));
}
});
// Set up your ActionBar
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
// You customization
final int actionBarColor = getResources().getColor(R.color.action_bar);
actionBar.setBackgroundDrawable(new ColorDrawable(actionBarColor));
final Button actionBarHome = (Button) findViewById(R.id.action_bar_title);
actionBarHome.setBackgroundResource(R.drawable.ic_action_back);
actionBarHome.setOnClickListener(this);
actionBarHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(intent2);
}
});
final Button actionBarInfo = (Button) findViewById(R.id.action_bar_staff);
actionBarInfo.setBackgroundResource(R.drawable.ic_action_help);
actionBarInfo.setOnClickListener(this);
actionBarInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(intent1);
}
});
}
//method called to update textview strings.
public String getMyString(int variable, Context applicationContext){
switch(variable){
case 1:
return applicationContext.getResources().getString(R.string.cutString);
break;
case 2:
return applicationContext.getResources().getString(R.string.markString);
break;
case 3:
return applicationContext.getResources().getString(R.string.measureString);
break;
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}