为了账号安全,请及时绑定邮箱和手机立即绑定

保存按钮状态(启用和禁用)

保存按钮状态(启用和禁用)

交互式爱情 2023-02-23 16:10:14
我在 android studio 中制作应用程序,在我的应用程序中我添加了功能,如果用户第二次单击按钮,则按钮被禁用并将按钮的状态保存在 sharedpreference 中,如果用户关闭应用程序并再次打开app 然后显示保存按钮状态(如果按钮被禁用,则显示禁用按钮,否则显示启用状态)。我的代码里放了很多sharedprefences的代码,但是每次都会出现null object reference。我的代码在下面给出,我把共享首选项代码放在这个按钮上,但是怎么做呢?爪哇: button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                counrClick = counrClick + 1;                if (counrClick == 1) {                    downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);                    Uri uri = Uri.parse("Url");                    DownloadManager.Request request = new DownloadManager.Request(uri);                    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);                    request.setAllowedOverRoaming(false);                    request.setTitle("" + "" + "");                    request.setDescription("Downloading " + "" + "");                    request.setVisibleInDownloadsUi(true);                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);                    Long reference = downloadManager.enqueue(request);                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/" + "filename");                    refid = downloadManager.enqueue(request);                    Log.e("OUT", "" + refid);                    if (counrClick == 2) {                        button.setEnabled(false);                    }                }            }        });
查看完整描述

3 回答

?
UYOU

TA贡献1878条经验 获得超4个赞

请参考下面的代码。请记住,您可以使用首选项名称 ( "MY_PREF") 和键名 ( "DOWNLOAD_BUTTON_STATUS") 来更改应用程序中其他任何位置的首选项。您甚至可以创建一个单独的类来控制应用程序中的所有首选项。


 private SharedPreferences sharedPreferences;

private Button btn_download_one, btn_download_two, btn_download_three, btn_download_four;

private final String DOWNLOAD_BUTTON_STATUS_KEY_ONE = "DOWNLOAD_BUTTON_STATUS_ONE";

private final String DOWNLOAD_BUTTON_STATUS_KEY_TWO = "DOWNLOAD_BUTTON_STATUS_TWO";

private final String DOWNLOAD_BUTTON_STATUS_KEY_THREE = "DOWNLOAD_BUTTON_STATUS_THREE";

private final String DOWNLOAD_BUTTON_STATUS_KEY_FOUR = "DOWNLOAD_BUTTON_STATUS_FOUR";

private int clickCountOne = 0, clickCountTwo = 0, clickCountThree = 0, clickCountFour = 0;


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    btn_download_one = findViewById(R.id.button1);

    btn_download_two = findViewById(R.id.button2);

    btn_download_three = findViewById(R.id.button3);

    btn_download_four = findViewById(R.id.button4);

    sharedPreferences = getSharedPreferences("MY_PREF", 0);

    btn_download_one.setEnabled(getDownloadButtonStatusPref(DOWNLOAD_BUTTON_STATUS_KEY_ONE));

    btn_download_two.setEnabled(getDownloadButtonStatusPref(DOWNLOAD_BUTTON_STATUS_KEY_TWO));

    btn_download_three.setEnabled(getDownloadButtonStatusPref(DOWNLOAD_BUTTON_STATUS_KEY_THREE));

    btn_download_four.setEnabled(getDownloadButtonStatusPref(DOWNLOAD_BUTTON_STATUS_KEY_FOUR));



    btn_download_one.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            //... some code

            clickCountOne++;

            if (clickCountOne == 2)

                changeDownloadButtonStatusPref(DOWNLOAD_BUTTON_STATUS_KEY_ONE, false);


        }

    });

    btn_download_two.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            //... some code

            clickCountTwo++;

            if (clickCountTwo == 2)

                changeDownloadButtonStatusPref(DOWNLOAD_BUTTON_STATUS_KEY_TWO, false);


        }

    });

    btn_download_three.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            //... some code

            clickCountThree++;

            if (clickCountThree == 2)

                changeDownloadButtonStatusPref(DOWNLOAD_BUTTON_STATUS_KEY_THREE, false);


        }

    });

    btn_download_four.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            //... some code

            clickCountFour++;

            if (clickCountFour == 2)

                changeDownloadButtonStatusPref(DOWNLOAD_BUTTON_STATUS_KEY_FOUR, false);


        }

    });


}


private void changeDownloadButtonStatusPref(String key, boolean status) {

    sharedPreferences.edit().putBoolean(key, status).apply();

    switch (key) {

        case DOWNLOAD_BUTTON_STATUS_KEY_ONE:

            btn_download_one.setEnabled(status);

            clickCountOne = 0;

            break;

        case DOWNLOAD_BUTTON_STATUS_KEY_TWO:

            btn_download_two.setEnabled(status);

            clickCountTwo = 0;

            break;

        case DOWNLOAD_BUTTON_STATUS_KEY_THREE:

            btn_download_three.setEnabled(status);

            clickCountThree = 0;

            break;

        case DOWNLOAD_BUTTON_STATUS_KEY_FOUR:

            btn_download_four.setEnabled(status);

            clickCountFour = 0;

            break;

    }

}


private boolean getDownloadButtonStatusPref(String key) {

    return sharedPreferences.getBoolean(key, true);

}



查看完整回答
反对 回复 2023-02-23
?
千巷猫影

TA贡献1829条经验 获得超7个赞

//在按钮点击时添加此代码


SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

        prefs.edit().putString("enabled", "").apply();

//在OnCreate/OncreateView方法中添加这段代码


  String statusLocked1 =  prefs.getString("enabled","");

    if(statusLocked1.equals("enabled")){

        //enable the button 

    }else{

        //disbale the button 

    }


查看完整回答
反对 回复 2023-02-23
?
SMILET

TA贡献1796条经验 获得超4个赞

试试这个,如果它之前被点击过两次,它会在你下次运行你的活动时禁用按钮;


Button button;

SharedPreferences preferences;

boolean firstclick = true;


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main2);


    // SharePrefs

    preferences = getSharedPreferences("yourprefsname", 0);

    firstclick = preferences.getBoolean("countclick", false);

    button = findViewById(R.id.yourbutton);


    //disables if it is clicked twice 

    if (!firstclick){

        button.setEnabled(false);

    }



    button.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            if (firstclick) {


                downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

                Uri uri = Uri.parse("Url");

                DownloadManager.Request request = new DownloadManager.Request(uri);

                request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);

                request.setAllowedOverRoaming(false);

                request.setTitle("" + "" + "");

                request.setDescription("Downloading " + "" + "");

                request.setVisibleInDownloadsUi(true);

                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

                Long reference = downloadManager.enqueue(request);

                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/" + "filename");

                refid = downloadManager.enqueue(request);

                Log.e("OUT", "" + refid);

                else{

                    //edit prefs                   

             preferences.edit().putBoolean("countclick",firstclick).apply();

                    button.setEnabled(false);


                }



            }

        }

    });

}


查看完整回答
反对 回复 2023-02-23
  • 3 回答
  • 0 关注
  • 118 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信