Android onClick method doesn't work on a custom view

后端 未结 10 1897
南旧
南旧 2021-01-01 09:32

I\'ve started working on an app. I build the menu yesterday but the onClick method doesn\'t work! I created a class that extends View and called her MainMenuObject - that cl

10条回答
  •  [愿得一人]
    2021-01-01 10:13

    I've got a solution! It's not really a solution for this specific issue, but a whole new approach. I sent this thread to somebody I know and he told me to use the Animation SDK the android has (like Wireless Designs mentioned), so instead of doing the main menu page with 4 classes, I'm doing it only with one class that extends Activity, and the Animation class offers many animation options. I want to thank you both for helping me, you are great. I'm adding the code if someone will encounter this thread with the same problem or something:

    package elad.openapp;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.HapticFeedbackConstants;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Window;
    import android.view.animation.Animation;
    import android.view.animation.AnimationSet;
    import android.view.animation.ScaleAnimation;
    import android.view.animation.TranslateAnimation;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    public class OpeningTimes extends Activity implements OnClickListener{
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        // Disabling the title bar..
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
    
        // Create the buttons and title objects
        ImageView title = (ImageView)findViewById(R.id.title_main);
        ImageView search = (ImageView)findViewById(R.id.search_button_main);
        ImageView support = (ImageView)findViewById(R.id.support_button_main);
        ImageView about = (ImageView)findViewById(R.id.about_button_main);
    
        // Setting the onClick listeners
        search.setOnClickListener(this);
        support.setOnClickListener(this);
        about.setOnClickListener(this);
    
        setButtonsAnimation(title, search, support, about);
    
    }
    
    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.search_button_main){
            v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
            startActivity(new Intent(this,SearchPage.class));
        }
        else if(v.getId()==R.id.support_button_main){
            v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
            Toast.makeText(this, "Coming soon...", Toast.LENGTH_LONG).show();
        }
        else if(v.getId()==R.id.about_button_main){
    
            v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
            Toast.makeText(this, "Coming soon...", Toast.LENGTH_LONG).show();
        }
    }
    
    // Setting the animation on the buttons
    public void setButtonsAnimation(ImageView title, ImageView search, ImageView support, ImageView about){
        // Title animation (two animations - scale and translate)
        AnimationSet animSet = new AnimationSet(true);
    
        Animation anim = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);
        anim.setDuration(750);
        animSet.addAnimation(anim);
    
        anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        anim.setDuration(750);
        animSet.addAnimation(anim);
    
        title.startAnimation(animSet);
    
        // Search button animation
        anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.5f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        anim.setDuration(750);
    
        search.startAnimation(anim);
    
        // Support button animation
        anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.5f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        anim.setDuration(750);
    
        support.startAnimation(anim);
    
        // About button animation
        anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 3f, Animation.RELATIVE_TO_SELF, 0.0f);
        anim.setDuration(750);
    
        about.startAnimation(anim);
    }
    }
    

提交回复
热议问题