如何在按钮单击事件上实现图像和文本切换



我正在尝试实现一个按钮单击事件以在图像切换器中切换图像,并在按钮单击时更新文本视图字符串。到目前为止,我已经让它适用于图像,但是当我尝试修改方法以包括更新字符串时,我收到以下错误:

  1. 在getMyString调用中,我得到以下内容:AboutActivity 类型中的方法 getMyString(int, Context) 不适用于 参数 (整数)

  2. 在getMyString的方法头上,这个:这一行有多个标记

    • 令牌"(", ;预期
    • 令牌上的语法错误")", ;预期
  3. 我在开关中调用字符串的地方是:void 方法无法返回值

    • 参数 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
    }

}

1.在getMyString调用中,我得到以下结果:AboutActivity 类型中的方法 getMyString(int, Context) 不适用于参数 (int)

你有

 tView.setText(getMyString(clicks++));

正如错误所说,仅将其传递int,但他们的方法需要(int, Context).所以应该是

 tView.setText(getMyString(clicks++, v.getContext()));

以便您向该方法传递正确的params

2在getMyString的方法头上,这个:这一行有多个标记 - 令牌"(",;预期 - 令牌上的语法错误")", ;预期

看起来您的getMyString()方法onCreate()或其他方法中。将其移出任何方法。

3.我在开关中调用字符串的地方是:void 方法无法返回值 - 参数 getMyString 的非法修饰符; 只允许 final。

我认为这也是由于您的方法在另一种方法中,但是它的设置方式有点令人困惑。进行这些更改并再次运行,看看您遇到了哪些错误。

编辑

您需要让该方法return String,现在如果cases都不正确,它将不会返回一个. Therefore, you can have a final如果它们不匹配where you return a default则返回 String' 值。或者你可以做这样的事情

/method called to update textview strings.
public String getMyString(int variable, Context applicationContext){
    String text = "";  // create a String variable to return and give it whatever 
                       // value you want in case none of the cases return true
    switch(variable){  // assign the variable below
        case 1:
            text = applicationContext.getResources().getString(R.string.cutString);
            break;
        case 2:
            text = applicationContext.getResources().getString(R.string.markString);
            break;
        case 3:
            text = applicationContext.getResources().getString(R.string.measureString);
            break;
    }
  return text; //just one return
}

我认为一个错误如下:在onClick()函数中,你调用getMyString函数,整数点击作为唯一的输入参数,而在getMyString函数的定义中,你说该函数必须接收一个整数和应用程序上下文。我建议像这样调用getMyString函数:

 tView.setText(getMyString(clicks++, getApplicationContext()));

在getMyString调用中,我得到以下结果:该方法 getMyString(int, Context) 类型为 AboutActivity 不适用 对于参数 (int)

因为您没有仅获取整数作为参数的方法。您的方法得到 2 个参数:

String getMyString(int variable, Context applicationContext)

无论如何解决方案;更改此内容:

tView.setText(getMyString(clicks++));

对此:

tView.setText(getMyString(clicks++), v.getContext());

最新更新