简体   繁体   English

如何检查TextView是否包含来自数组的字符串?

[英]How to check if a TextView contains Strings from an Array?

So, let's say I have an Array: 因此,假设我有一个数组:

String Array[] = {"Dog goes woof", "Cat goes meow", "Cow goes moo", "Etc..."};

And I have a Button and a TextView connected so that when the button is pressed I can say for example: 而且我有一个Button和一个TextView连接,这样当按下按钮时,我可以说例如:

public void onClick(View v) {
     TextView.setText(Array[1]);
}

Which will then fill the TextView with "Cat goes meow". 然后用“猫叫声”填充TextView。 But I now want the button to decided what should fill the TextView based on what's already there. 但是我现在希望按钮根据已经存在的内容来决定应填充TextView的内容。 So let's say I have Array[0] displayed in the TextView, so that it says "Dog goes woof". 假设我在TextView中显示了Array [0],因此显示为“ Dog go woof”。 I then want an if-test that recognizes that Array[0] is displayed, and then changes it to, say, Array[2]. 然后,我想要一个if测试,该测试可以识别出Array [0],然后将其更改为Array [2]。 And if Array[2] is displayed, it will then change it to Array[1], and so on. 并且如果显示Array [2],则它将其更改为Array [1],依此类推。

I am having a bit of issues on how I should set up this if-test, and how I should phrase it so that it recognizes what it is I am looking for in the TextView. 我在如何设置此if测试以及如何对其措辞方面存在一些问题,以便它可以识别我在TextView中寻找的内容。 And I do want to look for Array[X] or Array[Y] and not the strings themselves like "Dog goes woof". 而且我确实要查找Array [X]或Array [Y],而不是像“ Dog go woof”这样的字符串本身。 Basically something that says (warning: poor seudo-code ahead): 基本上是这样说的(警告:前面的伪代码很差):

if TextView contains Array[X]
then TextView.setText(Array[Y])
else if TextView contains Array[Y]
then TextView.setText(Array[Z])
else TextView.setText(Array[X])

I easily understood your requirement and here's what you need: 我很容易理解您的要求,这是您需要的:

    final Button yourButton= findViewById(R.id.yourButtonID);
    final String Array[] = {"Dog goes woof", "Cat goes meow", "Cow goes moo", "Etc..."};
    final TextView yourTextView = findViewById(R.id.yourTextViewID);
    yourButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int position = 0;
            if(textView.getText() == null)
            {
                textView.setText(Array[1]); // initial value
            }
            else{
                for(int i=0;i<Array.length;i++){
                  if(Objects.equals(textView.getText(),Array[i])){
                    position=i; //got the selected position
                    break;
                  }
                }
                if(position<Array.length-1){
                    textView.setText(Array[position+1]); 

                }
                else{
                    textView.setText(Array[position-1]);
                }
            }
        }
    });

This will check whether there is an initial value and in case of one then it will check for the selected position and if the position is less than the last item then it will increment one and if the last item is selected it will decrement one. 这将检查是否有一个初始值,如果是一个初始值,则将检查所选位置,如果该位置小于最后一项,则它将增加一个,如果选择了最后一项,则将减少一个。 I've tested it and it's working, still something feel free to ask. 我已经对其进行了测试,并且可以正常工作,但是仍然可以随意询问。

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

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