用户单击生成按钮时生成N个随机的4位数整数字符串和7个字符的字符串,并在用户单击显示按钮时显示全部字符串

时间:2018-07-11 23:25:54

标签: java android android-sharedpreferences

N个键和值是随机生成的。我测试了基于用户输入N,LB和UB生成4个数字字符串(称为键)和7个字符串(称为值)的代码。现在,我想使用共享首选项显示键和值。当我运行项目时,每次仅显示1个键和值。也许我没有正确实现共享首选项。

这是我的main_activity.java

代码
package com.rough.problem.problem6;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableRow;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    EditText n,lb,ub;
    Button generate, show, quit;
    String number;
    String generatednum;
    String random_string;
    int length = 7;
    int num,min,max;
    SharedPreferences preferences = getSharedPreferences("temp", getApplicationContext().MODE_PRIVATE);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        n = (EditText) findViewById(R.id.num);
        lb = (EditText) findViewById(R.id.lowerbound);
        ub = (EditText) findViewById(R.id.upperbound);

        generate= (Button) findViewById(R.id.Generate);
        show = (Button) findViewById(R.id.Show);
        quit = (Button) findViewById(R.id.Quit);


        generate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                num = Integer.parseInt(n.getText().toString());
                min = Integer.parseInt(lb.getText().toString());
                max = Integer.parseInt(ub.getText().toString());


                for (int i = 1; i <= num; i++) {

                    Random random = new Random();
                    generatednum = String.format("%04d", random.nextInt(max + 1 - min) + min);

                   // Key.append(generatednum + "\n");


                    char[] chars1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
                    StringBuilder sb1 = new StringBuilder();
                    Random random1 = new Random();
                    for (int j = 0; j < length; j++)
                    {
                        char c1 = chars1[random1.nextInt(chars1.length)];
                        sb1.append(c1);
                    }
                    random_string = sb1.toString();

                    //  Value.append(random_string + "\n");


                   // editor.commit();
                }
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("key", generatednum);

                editor.putString("value",random_string);
            }
        });

        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent keyIntent = new Intent(MainActivity.this, key.class);
                MainActivity.this.startActivity(keyIntent);
                startActivity(keyIntent);
            }
        });




    }
}

还有key.java的代码,它应该显示使用共享首选项生成的键和值。

    package com.rough.problem.problem6;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;

public class key extends AppCompatActivity {
    Button Back;
    TextView Key, Value;
    int n,lb,ub;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_key);

        Back = (Button) findViewById(R.id.back);
        Key = (TextView) findViewById(R.id.key);
        Value = (TextView) findViewById(R.id.value);
        SharedPreferences preferences=getSharedPreferences("temp", getApplicationContext().MODE_PRIVATE);
        String key = preferences.getString("key",null);
        String value = preferences.getString("value",null);

        Key.append(key + "\n");
        Value.append(value + "\n");
    }
}

这是我的主要活动指南

enter image description here

这是Key.java活动屏幕截图

enter image description here

它仅显示1个键和值,我需要帮助。

1 个答案:

答案 0 :(得分:1)

它只显示一个键和值,因为您只存储一个键和值。尽管您的// Example 1: Simple timing // $startTime = `timerX`; // code that is being timed $totalTime = `timerX -startTime $startTime`; print ("Total Time: "+$totalTime+"\n"); 循环确实生成n个键和值,但是您的for代码在循环的末尾,因此仅存储最后生成的键和值。由于看起来您想将所有键存储在一个SharedPreferences字符串中,而所有值存储在另一个SharedPreferences字符串中,可以通过在“生成”按钮的SharedPreferences中添加几个变量来解决此问题。 :

OnClickListener