Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在按钮点击事件上实现图像和文本切换?_Android_Onclick_Textview_Switch Statement - Fatal编程技术网

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

Android 如何在按钮点击事件上实现图像和文本切换?,android,onclick,textview,switch-statement,Android,Onclick,Textview,Switch Statement,我正在尝试实现一个按钮单击事件,以在图像切换器中切换图像,并在按钮单击时更新textView字符串。到目前为止,我已将其用于图像,但当我尝试修改该方法以包括更新字符串时,我收到以下错误: 在getMyString调用中,我得到以下结果:AboutActivity类型中的getMyString(int,Context)方法不适用于 参数(int) 在getMyString的方法头上:此行有多个标记 标记“(”,)上出现语法错误;应为 令牌“)”上的语法错误;期望 我在开关中调用字符串的地方是:

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

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

  • 在getMyString的方法头上:此行有多个标记

    • 标记“(”,)上出现语法错误;应为
    • 令牌“)”上的语法错误;期望
  • 我在开关中调用字符串的地方是:Void方法不能返回值

    • 参数getMyString的修饰符非法;唯一的决赛是 允许的
  • 有人知道我在这个实现中出了什么问题吗?我想可能是缺少大括号,或者代码放在了上下文之外,但是从错误的角度看,似乎是我在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()));
    
    这样,您就可以将该方法所采用的正确的
    参数传递给它

    2在getMyString this的方法头处:此行有多个标记-令牌上的语法错误(“,;应为-令牌上的语法错误”);期望

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

    3.我在开关中调用字符串的地方是:Void方法不能为参数getMyString返回值-非法修饰符;只允许决赛

    我认为这也是因为你的方法是在另一个方法中,但是它的设置方式有点让人困惑。进行这些更改,然后再次运行,看看会出现什么错误

    编辑

    您需要使用方法
    return
    a
    String
    ,如果
    案例
    中没有一个是真的
    ,它现在就不会返回一个。因此,您可以有一个最终的
    return
    ,如果它们不匹配,则返回一个默认的
    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函数的定义中,您说函数必须接收整数和applicationContext。我建议如下调用getMyString函数:

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

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

    String getMyString(int variable, Context applicationContext)
    
    无论如何,解决方案;更改此项:

    tView.setText(getMyString(clicks++));
    
    为此:

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

    好的,这修复了第一个错误。我将该方法放在onCreate()之外,但它仍然给出错误,第二个错误是:该方法必须返回字符串类型的结果,最后一个错误是:“无法访问的代码”。如果我在上面的问题中发布完整的课程,你能看一下吗?ThanksI发布了完整的类,以更好地了解我正在尝试做的事情。上面的内容已根据您的建议进行编辑,getMyString已移到onCreate()之外。请参阅我的编辑。我添加的代码应该与代码中的注释一起使用。现在只需将刷卡添加到应用程序中。我正试图找到一个关于它的好教程,但很难看到代码片段适合我的应用程序。你知道一个合适的吗?