【问题标题】:How to replace a target array string and change it to the user input string如何替换目标数组字符串并将其更改为用户输入字符串
【发布时间】:2020-08-12 14:14:06
【问题描述】:

所以基本上我是 stackoverflow 中的新人。我有这段代码,它将每个下划线替换为用户字符串。

public class MainActivity extends AppCompatActivity {
private String result="";
private String textresult = "The red fox jumps over the lazy dog";
EditText text;
Button btn;
public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = findViewById(R.id.editText);
        btn = findViewById(R.id.button_edittext);
        final TextView tvtext = findViewById(R.id.result);
        final String les = textresult.replaceAll("[a-z]", "_");
        tvtext.setText(les);

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

    String les1 = textresult.replaceAll("[a-z]", "_");
    final String sampleText = text.getText().toString().trim();
    int noOfBtns = sampleText.length();
    int noOftext = les1.length();


    final TextView[] btns = new TextView[noOfBtns];

    for(int i=0;i<noOfBtns;i++)
    {
        btns[i] =   new TextView(MainActivity.this);
        btns[i].setText(sampleText.substring(i,i+1));

        result = result+btns[i].getText().toString();
        char[] c = result.toCharArray();

          les1 = changeCharInPosition(i, c[i], les1);*/
                tvtext.setText(les1);
        }
    }
});
}

所以输出会是这样的:

**T__ ___ ___ ____ ____ ___ ____ ___.**

问题:那么我如何才能将文本的第一个长度定位到文本的末尾并更新或替换字符的每个长度,例如:

当用户输入单词并更新或替换时:

**user input: the red
display: the red ___ ____ ____ ___ ____ ___.**

如果用户输入了一个错误的字母,它会显示 *:

**user input: the red fix
display: the red f*x ____ ____ ___ ____ ___.**

此代码急需帮助。 谢谢!!

【问题讨论】:

  • “我怎样才能将文本的第一个长度定位到文本的末尾并更新或替换字符的每个长度”——如何替换字符的长度?那有什么意思?看来您需要有关刽子手游戏的字符串函数的帮助,而 Android 代码与您在这里提出的多个开放式问题完全无关。

标签: javascript java android android-studio android-layout


【解决方案1】:

我认为您要做的是根据来自 EditText 的当前用户输入更新所有这些 TextView。在 text 变量上添加 TextWatcher:

text.addTextChangedListener(new TextWatcher(){...

你必须自己弄清楚你需要什么样的 TextWatcher 接口方法。 祝你好运!

【讨论】:

    【解决方案2】:

    您的示例代码不是 JavaScript,但您这样标记了您的问题。

    所以我给出一个简单的 JS 答案:)

    const text = "The red fox jumps over the lazy dog."
    
    const question = document.getElementById('question')
    const guess = document.getElementById('guess')
    
    // handling input
    const check = (g, t) => {
      const gArr = [...g]
      const tArr = [...t]
    
      const r = []
    
      tArr.forEach((e, i) => {
        // if the character in the text is ' ' or '.'
        // just return it as it is
        if (e === ' ' || e === '.') {
          r.push(e)
        } else if (!gArr[i]) {
          // if the quess is shorter than the real text, then
          // return _ in not yet quessed places
          r.push('_')
        } else if (gArr[i]) {
          if (gArr[i] === e) {
            // if the guess is correct,
            // then return the correct guess
            r.push(e)
          } else {
            // if the guess is incorrect,
            // then return '*'
            r.push('*')
          }
        }
      })
      return r.join('')
    }
    
    // set the stage
    question.innerHTML = check(guess.value, text)
    
    // react to input events
    guess.addEventListener('input', function(e) {
      const r = check(e.target.value, text)
      question.innerHTML = r
    })
    <label for="guess">Guess the text: <input id="guess" type="text"/></label>
    <div id="question"></div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-20
      • 2013-11-08
      • 1970-01-01
      • 2015-10-03
      • 1970-01-01
      • 2015-09-23
      • 1970-01-01
      • 2021-09-01
      相关资源
      最近更新 更多