必须单击两次才能调用onclick

时间:2017-10-28 10:55:52

标签: android button onclick sharedpreferences

我尝试创建简介活动仅在用户点击跳过按钮后出现一次,跳过按钮允许用户转到的 mainactivity 我的问题是当我点击跳过按钮时没有得到任何东西,直到我按两次或三次,之后跳过按钮效果很好我真的不知道为什么Onclick方法不起作用 这是我的代码:

public class intro extends Activity {
   Button skip;
    SharedPreferences appPreferences;
    boolean isclick = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_intro);
        skip = (Button)findViewById(R.id.skip);
        hide();
        appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        isclick= appPreferences.getBoolean("isclick",false);
        if(isclick){
            Intent toMain = new Intent(this,MainActivity.class);
            startActivity(toMain);
        }
        skip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isclick){
                    Intent toMain = new Intent(intro.this,MainActivity.class);
                    startActivity(toMain);
                    finish();
                    SharedPreferences.Editor editor = appPreferences.edit();
                    editor.putBoolean("isclick", true);
                    editor.apply();
                }
            }
        });
    }

1 个答案:

答案 0 :(得分:1)

您的isClick逻辑对我没有意义。如果isclick为真,那么为什么没有关于是否在mainactivity中进行介绍的逻辑,而不是总是运行这个介绍,但是然后立即启动mainactivity。在主要活动中有这样的事情:

protected void onCreate(Bundle savedInstanceState) {

    SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    if (!appPreferences.getBoolean("introSkipped", false)) {
        Intent toIntro = new Intent(this,Intro.class);
        startActivity(toIntro);
    } else {
    // continue with main as normal
    }
}

然后,你跳过按钮的onClick变为:

        skip.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {                      
                SharedPreferences.Editor editor = appPreferences.edit();
                editor.putBoolean("introSkipped", true);
                editor.apply();
                finish();                
        }
    });