简体   繁体   English

使用 SHARED PREFERENCES android studio 更改按钮的文本

[英]CHANGING the text of a button with SHARED PREFERENCES android studio

I'm making a simple app that has a button.我正在制作一个带有按钮的简单应用程序。 When a pokemon is caught, I want the text to be "release" and when it's not, to be "catch.", When clicking the button.当一个口袋妖怪被捕获时,我希望文本是“释放”,当它不是时,是“捕获。”,当点击按钮时。 I want to change caught from true to false or vice-versa.我想将 catch 从 true 更改为 false,反之亦然。

For now, i've done this (it doesn't work as I expected):现在,我已经这样做了(它不像我预期的那样工作):

    public void toggleCatch(View view) {

        boolean caught;

        String name = nameTextView.getText().toString();

        SharedPreferences captured = getSharedPreferences("pokemon_name",Context.MODE_PRIVATE);

        if (captured.contains (name) ){
            catch_button.setText("Release");
            caught=true;
        }
        else{
            catch_button.setText("Catch!");
            caught=false;
        }

        if (caught) {
            getPreferences(Context.MODE_PRIVATE).edit().putString("pokemon_name", name).commit();
        } else {
            getPreferences(Context.MODE_PRIVATE).edit().remove(name).commit();
        }

    }

I would be very thankful if someone can help me!如果有人可以帮助我,我将非常感激!

I'm lost so I don't know if I'm on the correct path, my code is probably completely wrong.我迷路了,所以我不知道我是否走在正确的道路上,我的代码可能完全错误。

SharedPreferences is a map of key-value pairs. SharedPreferences 是key-value对的 map。 So if you are trying to access the value you should check whether the key exists which in your case is pokemon_name .因此,如果您尝试访问该值,则应检查密钥是否存在,在您的情况下为pokemon_name

if (captured.contains("pokemon_name")){
    ...
}

And when removing once again you should give the key, not the value.再次删除时,您应该给出密钥,而不是值。

...edit().remove("pokemon_name").commit();

Read SharedPreferences official documentation to understand better.阅读SharedPreferences官方文档以更好地理解。

I guess here is what you want:我想这就是你想要的:

In your app, you have an EditText that allows users to input pokemon name.在您的应用程序中,您有一个允许用户输入口袋妖怪名称的EditText When users click on the toggle catch button,当用户点击切换捕捉按钮时,

  • If the pokemon name is captured (the pokemon name has been saved in the SharePreferences ), then remove the pokemon name from the SharePreferences and set the text of the button to "Release".如果精灵名称被捕获(精灵名称已保存在SharePreferences中),则从SharePreferences中删除精灵名称并将按钮的文本设置为“释放”。

  • If the pokemon name is not captured (the pokemon name hasn't been saved in the SharePreferences ), then add the pokemon name to the SharePreferences and set the text of the button to "Catch.".如果口袋妖怪名称未被捕获(口袋妖怪名称尚未保存在SharePreferences中),则将口袋妖怪名称添加到SharePreferences并将按钮的文本设置为“Catch.”。

Solution解决方案

public void toggleCatch(View view) {
    String name = nameTextView.getText().toString().trim();
    SharedPreferences captured = getSharedPreferences("pokemon_name", Context.MODE_PRIVATE);
    boolean caught = captured.contains(name);
    if (caught) {
        captured.edit().remove(name).apply();
        catch_button.setText("Release");
    } else {
        captured.edit().putBoolean(name, true).apply();
        catch_button.setText("Catch!");
    }
}

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

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