第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

保存按鈕狀態(tài)(啟用和禁用)

保存按鈕狀態(tài)(啟用和禁用)

交互式愛情 2023-02-23 16:10:14
我在 android studio 中制作應(yīng)用程序,在我的應(yīng)用程序中我添加了功能,如果用戶第二次單擊按鈕,則按鈕被禁用并將按鈕的狀態(tài)保存在 sharedpreference 中,如果用戶關(guān)閉應(yīng)用程序并再次打開app 然后顯示保存按鈕狀態(tài)(如果按鈕被禁用,則顯示禁用按鈕,否則顯示啟用狀態(tài))。我的代碼里放了很多sharedprefences的代碼,但是每次都會(huì)出現(xiàn)null object reference。我的代碼在下面給出,我把共享首選項(xiàng)代碼放在這個(gè)按鈕上,但是怎么做呢?爪哇: 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貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊

請(qǐng)參考下面的代碼。請(qǐng)記住,您可以使用首選項(xiàng)名稱 ( "MY_PREF") 和鍵名 ( "DOWNLOAD_BUTTON_STATUS") 來更改應(yīng)用程序中其他任何位置的首選項(xiàng)。您甚至可以創(chuàng)建一個(gè)單獨(dú)的類來控制應(yīng)用程序中的所有首選項(xiàng)。


 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);

}



查看完整回答
反對(duì) 回復(fù) 2023-02-23
?
千巷貓影

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊

//在按鈕點(diǎn)擊時(shí)添加此代碼


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 

    }


查看完整回答
反對(duì) 回復(fù) 2023-02-23
?
SMILET

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊

試試這個(gè),如果它之前被點(diǎn)擊過兩次,它會(huì)在你下次運(yùn)行你的活動(dòng)時(shí)禁用按鈕;


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);


                }



            }

        }

    });

}


查看完整回答
反對(duì) 回復(fù) 2023-02-23
  • 3 回答
  • 0 關(guān)注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)