Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 保存按钮状态(启用和禁用)_Java_Android_Sharedpreferences - Fatal编程技术网

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

Java 保存按钮状态(启用和禁用),java,android,sharedpreferences,Java,Android,Sharedpreferences,我在android studio中制作该应用程序,在我的应用程序中添加该功能,如果用户再次单击按钮,则该按钮为禁用,并在SharedPreference中保存按钮状态,如果用户关闭应用程序并再次打开应用程序,则显示保存按钮状态(如果按钮被禁用,则显示禁用按钮,否则显示启用状态)。我在代码中放入了许多SharedPreferences代码,但每次都出现空对象引用。下面给出了我的代码,我在此按钮上放入了共享首选项代码,但如何操作 爪哇: //单击按钮添加此代码 SharedPreferences p

我在android studio中制作该应用程序,在我的应用程序中添加该功能,如果用户再次单击按钮,则该按钮为禁用,并在SharedPreference中保存按钮状态,如果用户关闭应用程序并再次打开应用程序,则显示保存按钮状态(如果按钮被禁用,则显示禁用按钮,否则显示启用状态)。我在代码中放入了许多SharedPreferences代码,但每次都出现空对象引用。下面给出了我的代码,我在此按钮上放入了共享首选项代码,但如何操作

爪哇:


//单击按钮添加此代码

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 
    }

尝试此操作,它将在下次运行活动时禁用按钮(如果以前单击过两次)

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

                }


            }
        }
    });
}

请参考下面的代码。请记住,您可以使用首选项名称(
“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);
}

你能发布sharedpref代码吗?@VladyslavMatviienko可能重复,你怎么能说上面提供给你的链接重复?我看不到任何与共享相关的东西,也看不到你在哪里使用逻辑来检查条件,并在应用程序打开后使按钮启用或禁用?如果你能提供正确的解决方案,那将是很好的相关代码片段的详细信息。@Harinja通过阅读问题当然:
但是每次出现空对象引用时。
错误是由以下原因引起的:java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.view$OnClickListener)'在一个空对象引用上,上述代码不起作用,在应用程序关闭时未保存buton状态i阶段当我在应用程序中放置4个按钮时又出现一个问题如果我打开应用程序时单击一个按钮并关闭应用程序,则按钮状态在4个按钮上应用,而不是在一个按钮上,当我打开应用程序时只需取消一个按钮,而不是下一个3个按钮。'出现错误的原因是:java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.view$OnClickListener)“在一个空对象引用上–Hassan Ali Mughal刚才我又遇到了一个问题我在我的应用程序中使用了你的代码,但只在一个按钮上使用了这些代码,但在mutliple button上没有。当我在应用程序中放置4个按钮时,如果我打开应用程序时单击一个按钮并关闭应用程序,则按钮状态为应用于4个按钮,而不是一个按钮,当我打开应用程序时,希望p然后只有一个按钮是disble而不是next 3按钮立即检查,您可以使用相同的方法支持更多按钮。如果您花一些时间检查和重构代码,您可以减少样板代码。
 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);
}