繁体   English   中英

使用onFling更改Imagebutton图像

[英]Change Imagebutton Image using onFling

我只是在玩Android Studio,所以我一直在硬编码手势完成后我希望看到的更改。 因此,我知道按钮不会从jabong更改为amazon.com(尽管有一些帮助使其动态也会有所帮助!)。

但是在此之前,我遇到的麻烦是在执行onFling操作时更改了ImageButton的图像。

原始图像位于drawable文件夹中,因此我也想将其更改为该图像。

我的代码如下。

public class MainActivity extends ActionBarActivity implements GestureDetector.OnGestureListener {

private TextView expiry;
private EditText JabongImpact;
private GestureDetectorCompat gestureDetector;
private ImageButton offer_image;
private Button storebutton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    JabongImpact = (EditText) findViewById(R.id.JabongImpact);
    expiry = (TextView) findViewById(R.id.expiry);
    offer_image = (ImageButton) findViewById(R.id.offer_image);
    this.gestureDetector = new GestureDetectorCompat(this, this);
    Button mybutton = (Button) findViewById(R.id.storebutton);

    mybutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse("http://www.jabong.com");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });
    offer_image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse("http://www.jabong.com");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }

    });

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    JabongImpact.setText("Impact shop with Amazon today!");
    expiry.setText("expires: tomorrow!");
    storebutton.setText("AMAZON.IN");
    return true;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    this.gestureDetector.onTouchEvent(event);
    return super.onTouchEvent(event);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

您可以通过这种方式更改图像

offer_image.setImageResource(R.drawable.<your drawable image>); 

暂无
暂无

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

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