简体   繁体   English

如何实现按钮单击事件中的图像和文本切换?

[英]How to implement an image and text switch on button click event?

I'm trying to implement a button click event to switch images in an image switcher along with updating textView strings on button click.So far I've got it working for the image,but when I tried to modify the method to include updating the strings I'm getting the following errors: 我正在尝试实现一个按钮单击事件,以在图像切换器中切换图像,并在按钮单击时更新textView字符串。到目前为止,我已经使它适用于图像,但是当我尝试修改方法以包括更新字符串我遇到以下错误:

  1. On the getMyString call I get the following:The method getMyString(int, Context) in the type AboutActivity is not applicable for the arguments (int) 在getMyString调用上,我得到以下信息:AboutActivity类型的方法getMyString(int,Context)不适用于参数(int)

  2. At the method header of getMyString this: Multiple markers at this line 在getMyString的方法标头上,此:这行有多个标记

    • Syntax error on token "(", ; expected 令牌“(”,;上的语法错误
    • Syntax error on token ")", ; 令牌“)”上的语法错误,; expected 预期
  3. And where I'm calling the strings in the switch this: Void methods cannot return a value 而我在开关中调用字符串的地方是:无效方法无法返回值

    • Illegal modifier for parameter getMyString; 参数getMyString的非法修饰符; only final is permitted 只允许决赛

Does anyone know where I'm going wrong with this implementation?I thought it may have been missing braces or that the code was placed out of context,but from looking at the errors it seems to be the way in which I'm calling the getMyString() in the onClick event. 有没有人知道我在实现上出错了?我以为它可能丢失了花括号,或者代码不在上下文中,但是从查看错误来看,这似乎是我所谓的方法getMyString()在onClick事件中。

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.On the getMyString call I get the following:The method getMyString(int, Context) in the type AboutActivity is not applicable for the arguments (int) 1.在getMyString调用上,我得到以下信息:AboutActivity类型的getMyString(int,Context)方法不适用于参数(int)

You have 你有

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

only passing it an int as the error says but them method takes (int, Context) . 仅将其传递给int如错误所述,但它们的方法采用(int, Context) So it should be 所以应该

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

So that you are passing it the correct params that the method takes. 这样您就可以将方法采用的正确params传递给它。

2At the method header of getMyString this: Multiple markers at this line - Syntax error on token "(", ; expected - Syntax error on token ")", ; 2在getMyString的方法标头上:此行有多个标记-标记“(”,;上的语法错误;预期-标记“)”上的语法错误,; expected 预期

It looks like your getMyString() method is inside onCreate() or some other method. 看起来您的getMyString()方法位于onCreate()或其他方法中。 Move it outside of any method. 将其移动到任何方法之外。

3.And where I'm calling the strings in the switch this: Void methods cannot return a value - Illegal modifier for parameter getMyString; 3,在开关中调用字符串的地方:无效方法不能返回值-参数getMyString的非法修饰符; only final is permitted 只允许决赛

I think this is also due to your method being inside of another method but the way its set up its a little confusing what's going on. 我认为这也是由于您的方法位于另一种方法内部,但是其设置方式使正在发生的事情有些混乱。 Make these changes and run again and see what errors you get. 进行这些更改,然后再次运行,看看会遇到什么错误。

Edit 编辑

You need to have the method return a String and right now it won't return one if none of the cases are true . Therefore, you can have a final 您需要让该方法return一个String ,现在,如果所有cases都不为真,则该方法将不返回一个String . Therefore, you can have a final . Therefore, you can have a final return where you return a default String` value in case non of them match. . Therefore, you can have a final返回值,如果它们都不匹配where you return a default String值。 Or you can do something like this 或者你可以做这样的事情

/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

}

I think one error is the following: in the onClick() function, you call the getMyString function with the integer clicks as the only input parameter, while in the definition of the getMyString function, you say that the function has to receive an integer AND the applicationContext. 我认为以下是一个错误:在onClick()函数中,您将整数单击作为唯一输入参数调用getMyString函数,而在getMyString函数的定义中,您说该函数必须接收整数AND applicationContext。 I would suggest to make the call of the getMyString function like this: 我建议像这样进行getMyString函数的调用:

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

On the getMyString call I get the following:The method getMyString(int, Context) in the type AboutActivity is not applicable for the arguments (int) 在getMyString调用上,我得到以下信息:AboutActivity类型的方法getMyString(int,Context)不适用于参数(int)

Becasuse you don't have a method which only gets an integer as a parameter. 因为您没有一个只能获取整数作为参数的方法。 Your method get 2 paramters: 您的方法获得2个参数:

String getMyString(int variable, Context applicationContext)

Anyway solution; 无论如何解决; change this: 改变这个:

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

To this: 对此:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM