【问题标题】:increase button when value coming from sharedpreferences当值来自 sharedpreferences 时增加按钮
【发布时间】:2013-09-20 08:43:37
【问题描述】:

我在 Activity 中有 editText。我使用 SharedPreference 来保存此值并稍后获取。我将此edittext 值传递给另一个活动按钮文本。当文本来自edittext时,我需要增加按钮。但问题是如果我输入文本,测试然后按钮是用测试文本创建的。如果我创建 test1,则按钮测试将替换为 test1。我需要用两个按钮显示 test 和 test1。

代码:

活动:

et=(EditText)findViewById(R.id.et);

        et1=(EditText)findViewById(R.id.et1);

        btn=(Button)findViewById(R.id.btn);

        btn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {

    SharedPreferences preferences = getSharedPreferences("sample",0);
                      SharedPreferences.Editor editor = preferences.edit();
                     editor.putString("Name",et.getText().toString());
                     editor.putString("Name1",et1.getText().toString());

                      editor.commit();

                      Intent intent = new Intent(Activity.this, Activity1.class);
                      startActivity(intent);

}


});

活动1:

LinearLayout layout = (LinearLayout) findViewById(R.id.linear);

        SharedPreferences preferences = getSharedPreferences("sample",0);
        String Namestr=(preferences.getString("Name",""));

        String newString="";

          if(Namestr.length()>0&& Namestr.equalsIgnoreCase(newString)){

              newString=Namestr;

              Button button = new Button(Get.this);

             // button.setText("hellow");
              button.setText(preferences.getString("Name", ""));
            layout.addView(button);

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_marginTop="0dp"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/linear"    >

它只显示一个按钮。如果我输入 test 它会显示带有测试文本的按钮,然后我输入 test2 它会用 test2 替换测试。我不想用 test2 替换我需要创建新按钮的文本。

【问题讨论】:

  • sharedPreference 的 b'coz 将值保留在密钥对中。因此,对于 1 个键,只会更新一个值。所以最好使用 Table 来存储 EditText 值。
  • 你能检查我编辑的问题吗?我如何存储每次edittext在表中输入值
  • 但是 String Namestr=(preferences.getString("Name",""));这将只返回单个值。它不是一个字符串数组。
  • 如何使用edittext中的多个值?
  • 请看我的回答..

标签: android sharedpreferences android-edittext counter android-button


【解决方案1】:

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final EditText et = (EditText) findViewById(R.id.et);
        EditText et1 = (EditText) findViewById(R.id.et1);
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final Map<String, String> demomap = new HashMap<String, String>();
                demomap.put(et.getText().toString(), et.getText().toString());
                SharedPreferences preferences = getSharedPreferences("sample",
                        0);
                SharedPreferences.Editor editor = preferences.edit();
                for (Entry<String, String> entry : demomap.entrySet()) {
                    editor.putString(entry.getKey(), entry.getValue());
                }
                editor.commit();

                Intent intent = new Intent(MainActivity.this,
                        secondactvity.class);
                startActivity(intent);

            }
        });

    }

}

secondActivity.java

public class secondactvity extends Activity {

    private HashMap<String, String> demomap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button back = (Button) findViewById(R.id.button2);
        demomap = new HashMap<String, String>();
        SharedPreferences preferences = getSharedPreferences("sample", 0);
        for (Entry<String, ?> entry : preferences.getAll().entrySet()) {
            demomap.put(entry.getKey(), entry.getValue().toString());
        }
        LinearLayout layout = (LinearLayout) findViewById(R.id.linear);
        Iterator myVeryOwnIterator = demomap.keySet().iterator();
        while (myVeryOwnIterator.hasNext()) {
            String key = (String) myVeryOwnIterator.next();
            String value = (String) demomap.get(key);
            Button button = new Button(this);
            button.setText(value);
            layout.addView(button);
        }

        back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();

            }
        });
    }
}

现在我希望它的问题得到解决。

【讨论】:

  • 现在什么都显示了。我们还需要显示带有更改比例的按钮。
  • 您可以更改按钮的任何属性。您可以显示任何您想要的控件。
  • 没有显示。你能看到我用 xml 代码编辑的问题吗
【解决方案2】:

你必须像动态创建按钮,

按钮 btn=new Button(this); //根据需要设置按钮属性和布局参数。

【讨论】:

  • 你没有理解我的问题。
  • 为此您必须编写代码来区分来自edittext的值。当您发现差异时比较旧值和新值创建新按钮。
【解决方案3】:

尝试以下代码作为参考,并开发您自己的逻辑。

 String newString="";

     if(Namestr.length()>0 && Namestr.equalIgnoreCase(newString)){

    newString=namestr;
    //YOUR STUFF

    }

【讨论】:

    【解决方案4】:

    现在我创建了不同的密钥来存储您的数据。 说 Name1, Name2 同样明智

    SharedPreferences preferences = getSharedPreferences("sample",0);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putInt("Count", 5);//say
    
                for(int i=1; i<=5;i++)
                {
                    editor.putString("Name"+i,et.getText().toString());
                }
    
                editor.commit();
    

    用于检索

    SharedPreferences preferences = getSharedPreferences("sample",0);
            int count=preferences.getInt("Count", 0);
            for(int i=1;i<=count;i++)
            {
                Button button = new Button(this);
                button.setText(preferences.getString("Name"+i, ""));
                layout.addView(button);
            }
    

    希望这对你有用。 但不是 sharedPreference 而是使用数据库来存储它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-20
      • 2020-04-12
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多