简体   繁体   中英

How to replace a target array string and change it to the user input string

so basically I'm a new one in stackoverflow. I have this code which is replacing every underscore into user string.

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

so the output would be like this:

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

the problem: so how can I target the first length of the text until the end of the text and update or replace every length of the character which is for example:

when the user input a word and update or replace:

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

and if the user input a wrong letter for the word it will display *:

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

badly needed help for this code. thank you!!

I think the what youre trying to do is update all those TextViews based on current userinput from the EditText. Add a TextWatcher on your text variable:

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

you have to figure out yourself what method of the TextWatcher interface you will need. Good luck!

Your example code is not JavaScript, but you tagged your question as such.

So I give a simple JS answer:)

 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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