【问题标题】:Using Text Only Once Through Random Method通过随机方法仅使用一次文本
【发布时间】:2015-12-08 11:24:14
【问题描述】:

我是 Android 新手。我在按钮上的 TextView 中显示文本随机单击。在第一个文本视图标题和第二个标题的解释。我能够随机显示标题和解释,现在我想如果文本显示一次不应再次显示意味着它将被删除。这就是我卡住的地方。我无法删除文本。任何帮助将不胜感激。我在这里发布我的代码。

MainActivity.java

TextView text_heading,text_explain;
Button click;
Random random;
Integer [] array_heading ,array_explain ;
Integer int_text;

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    text_heading = (TextView) findViewById(R.id.text_heading);
    text_explain = (TextView) findViewById(R.id.text_explain);
    click = (Button) findViewById(R.id.click);

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

            text_heading.setText(array_heading.get(int_text)); //getting error
            text_explain(array_explain.get(int_text)); //getting error
            array_heading.remove(int_text); //getting error
            array_explain.remove(int_text); //getting error
        }
    });

    random = new Random();

    array_heading  = new Integer []{R.string.source_text1, R.string.source_text2, R.string.source_text3,
            R.string.source_text6, R.string.source_text5, R.string.source_text4, R.string.source_text7,
            R.string.source_text8, R.string.source_text9};

    array_explain = new Integer []{R.string.source_text1_explain, R.string.source_text2_explain,
            R.string.source_text3_explain,
            R.string.source_text4_explain, R.string.source_text5_explain, R.string.source_text6_explain,
            R.string.source_text7_explain,
            R.string.source_text8_explain, R.string.source_text9_explain};

    ArrayList<Integer> array_headingList = new ArrayList<Integer>(Arrays.asList(array_heading));

    ArrayList<Integer>array_explainList = new ArrayList<Integer>(Arrays.asList(array_explain));


    int_text = random.nextInt(array_headingList.size() - 1);
}
   }

【问题讨论】:

标签: android random textview


【解决方案1】:

我无法删除文本。任何帮助将不胜感激。我是 在这里发布我的代码。

要清理TextView 的内容,您可以将null 传递给setText。例如

text_heading.setText(null);

如果你想每次点击按钮都改变内容,你必须移动

int_text = random.nextInt(array_heading.length);

您的onClick 回调,

您应该知道下一个 int 返回一个介于 [0, n) 之间的 int 的事实。 array_heading.length -1 仅当您想从您想要显示的可能文本中排除 R.string.source_text9_explain 时才需要。还要记住,如果array_heading 包含的项目多于array_explain,您可能会得到ArrayIndexOutBoundException

【讨论】:

  • text_heading.setText(null);应该在所有文本都使用一次时显示...意味着我想在 textview 中使用这些文本一次...
  • array_heading 和 array_explain 都具有相同数量的项目....显示一次的文本不会重复....这就是我想要的
【解决方案2】:

我会将字符串放在一个对象中:

public class Item {
    private final int textId;
    private final int textExplanationId;

    public class Item(int textId, int textExplanationId){
        this.textId = textId;
        this.textExplanationId = textExplanationId;
    }

    public int getTextId(){return textId;}
    public int getTextExplanationId(){return textExplanationId;}
}

然后我会将它们存储在ArrayList

List<Item> items = new ArrayList<Item>(new Item[]{
     new Item(R.string.source_text1, R.string.source_text1_explain),
     new Item(R.string.source_text2, R.string.source_text2_explain),
      //etc
     });

然后我将洗牌该数组一次

Collections.shuffle(items);

并按顺序读取:

Item current = items.get(currentIndex++);
text_heading.setText(current.getTextId());
text_explain.setText(current.getTextExplanationId());

【讨论】:

  • 这样我总是会首先得到“R.string.source_text1”,但我希望每次用户启动应用程序时随机显示文本。
  • 这就是洗牌的目的
【解决方案3】:
TextView text_heading,text_explain;
Button click;
Random random;
Integer [] array_heading ,array_explain ;
Integer int_text;

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    text_heading = (TextView) findViewById(R.id.text_heading);
    text_explain = (TextView) findViewById(R.id.text_explain);
    click = (Button) findViewById(R.id.click);

    random = new Random();

    array_heading  = new Integer []{R.string.source_text1, R.string.source_text2, R.string.source_text3,
            R.string.source_text6, R.string.source_text5, R.string.source_text4, R.string.source_text7,
            R.string.source_text8, R.string.source_text9};

    array_explain = new Integer []{R.string.source_text1_explain, R.string.source_text2_explain,
            R.string.source_text3_explain,
            R.string.source_text4_explain, R.string.source_text5_explain, R.string.source_text6_explain,
            R.string.source_text7_explain,
            R.string.source_text8_explain, R.string.source_text9_explain};

    ArrayList<Integer> array_headingList = new ArrayList<Integer>(Arrays.asList(array_heading));

    ArrayList<Integer> array_explainList = new ArrayList<Integer>(Arrays.asList(array_explain));


    int_text = random.nextInt(array_headingList.size() - 1);

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

            text_heading.setText(array_headingList.get(int_text)); //getting error
            text_explain.setText(array_explainList.get(int_text)); //getting error
            array_headingList.remove(int_text); //getting error
            array_explainList.remove(int_text); //getting error
        }
    });

}
   }

【讨论】:

  • 已编辑。现在应该可以工作了。将数组转换为 ArrayList 时遗漏了一些步骤
  • Confusing--> 使用 ArrayList 而不是 int 数组 & 请注意,您必须将 int 数组的类型更改为 Integer Integer array_heading [], array_explain[]
  • 数组一旦创建就不能更改,ArrayList可以。由于您要删除元素和数组不适合您的目的。
  • 同样要使用 Arrays.asList() 你需要一个包装类数据类型而不是原始数据类型(在这种情况下是整数而不是 int)。你的 Java 似乎有点生疏了。请参考 ArrayList 和数组的 java 文档。
  • 再次编辑。另外我建议您将需要在java中使用的字符串直接声明为Java String,并且只将要在strings.xml中的xml文件中使用的字符串
【解决方案4】:

你必须使用 ArrayList。

ArrayList<Integer> heading = Arrays.asList(array_heading);
ArrayList<Integer> explain = Arrays.asList(array_explain);

现在从这个数组列表中设置文本。当它的集合一旦从arraylist中删除它就不能再显示了。

这样使用

random = new Random();

array_heading  = new Integer []{R.string.source_text1, R.string.source_text2, R.string.source_text3,
        R.string.source_text6, R.string.source_text5, R.string.source_text4, R.string.source_text7,
        R.string.source_text8, R.string.source_text9};

array_explain = new Integer []{R.string.source_text1_explain, R.string.source_text2_explain,
        R.string.source_text3_explain,
        R.string.source_text4_explain, R.string.source_text5_explain, R.string.source_text6_explain,
        R.string.source_text7_explain,
        R.string.source_text8_explain, R.string.source_text9_explain};

ArrayList<Integer> array_headingList = new ArrayList<Integer>(Arrays.asList(array_heading));

ArrayList<Integer> array_explainList = new ArrayList<Integer>(Arrays.asList(array_explain));

int_text = random.nextInt(array_headingList.size());

click.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        text_heading.setText(array_headingList.get(int_text)); 
        text_explain.setText(array_explainList.get(int_text)); 
        array_headingList.remove(int_text); 
        array_explainList.remove(int_text);
        if(array_headingList.size() == 0){
          click.setEnabled(false);
          Toast.makeText(getApplicationContext(),"All text finished",Toast.LENGTH_SHORT).show();
        } else if(array_headingList.size() == 1){
        int_text = 0;
        } else {
         int_text = random.nextInt(array_headingList.size());
        }
    }
});

【讨论】:

  • 文本在按钮点击时发生变化,但它会重复,并且您的 if 案例不起作用.....
  • 已编辑检查...由于随机数而重复。
  • 由于随机数而重复.....然后“删除”在此没有做任何事情
  • 我从一开始就随机获取文本......我主要关心的是删除曾经使用或显示过的文本......但仍然没有解决
  • 我已经测试了代码的工作原理。你遇到了哪些错误。?
猜你喜欢
  • 2016-03-11
  • 1970-01-01
  • 2015-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-14
相关资源
最近更新 更多