簡體   English   中英

通過隨機方法僅使用一次文本

[英]Using Text Only Once Through Random Method

我是Android新手。 我在按鈕上單擊隨機單擊的TextView中顯示文本。 在第一個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);
}
   }

我無法刪除這些文字。 任何幫助將不勝感激。 我在這里發布我的代碼。

要清除TextView的內容,可以將null傳遞給setText 例如

text_heading.setText(null);

如果您想在每次單擊按鈕時更改內容,則必須移動

int_text = random.nextInt(array_heading.length);

您的onClick回調,

您應該意識到下一個int返回[0, n)之間的int的事實。 僅當您要從要顯示的可能文本中排除R.string.source_text9_explain時,才需要array_heading.length -1 還請記住,如果array_heading包含的項目多於array_explain ,則可能會出現ArrayIndexOutBoundException

您必須為此使用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());
        }
    }
});
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
        }
    });

}
   }

我會將字符串放在一個對象中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM