简体   繁体   English

Android:单击其他ImageButtons时闪烁的屏幕

[英]Android: Flashing screen when clicked on different ImageButtons

I have 3 ImageButtons that i use for tabs, when they are clicked they load a new activity, but the problem i have is they feel "laggy" and if you click on them multiple times the screen starts flashing and eventually the app crashes, is there any way to get rid of the flashing screen or should i use another method for the tabs? 我有3个用于标签的ImageButton,单击它们会加载新的活动,但是我遇到的问题是它们感到“松懈”,如果多次单击它们,屏幕开始闪烁,最终应用崩溃有什么办法摆脱闪烁的屏幕,或者我应该对选项卡使用另一种方法吗?

Here is the code i use: 这是我使用的代码:

import android.app.Activity;

public class ContactsActivity extends Activity {

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

    final ImageButton b = (ImageButton) findViewById(R.id.imageButtonCall);
    b.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Intent intent = new Intent(ContactsActivity.this,CallActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_no_move, R.anim.fade);
        finish();
    }
});

    final ImageButton c = (ImageButton) findViewById(R.id.imageButtonSettings);
    c.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Intent intent = new Intent(ContactsActivity.this,SettingsActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_no_move, R.anim.fade);
        finish();
    }
});

What about try to make buttons unable to be clicked more than one time at same time? 试图使按钮不能同时被多次单击该怎么办?

Put this in class as global variable: 将其作为全局变量放在类中:

boolean isBclicked = false;

Now rewrite onClickListener: 现在重写onClickListener:

final ImageButton b = (ImageButton) findViewById(R.id.imageButtonCall);
    b.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
         if( !isBclicked){
           isBclicked = true;
           Intent intent = new Intent(ContactsActivity.this,CallActivity.class);
           startActivity(intent);
           overridePendingTransition(R.anim.slide_no_move, R.anim.fade);
           finish();
         }
   }
});

So basically user won't be able to click multiple times on button, which means that animations won't be interrupted, so your problem will be solved. 因此,基本上用户将无法多次单击按钮,这意味着动画不会被打断,因此您的问题将得到解决。 You can do same thing with other buttons and because after starting new activity you finish current one, there's no need to set "isBclicked" back to false. 您可以使用其他按钮执行相同的操作,并且由于在开始新活动后您完成了当前活动,因此无需将“ isBclicked”设置回false。

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

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