开发者

How to jumble a word from EditText and apply the jumbled word into a TextView

I need to know how to jumble a word entered into EditText.

The jumbled word will show in another TextView in the same interface.

I have tried to do this but I get a force close error. This is what I have tried within the button:

wordE = (EditText)findViewById(R.id.entry); 
jumble = (TextView) findViewById(R.id.jumble);
Button link5Btn = (Button)findViewById( R.id.selected );
link5Btn.setOnClickListener( new View.OnClickListener()
{
public void onClick(View v)
    {
jumbleMe(al);
}

Which calls the method:

private void jumbleMe( String word ){

al = wordE.getText().toString(); 
ArrayList<Character> al = new ArrayList<Character>();
for (int i = 0; i < wordE.length(); i++) {
    al.add(word.charAt(i));
}
Collections.shuffle(al);

jumble.setText( al.toString() );
}

I would appreciate an开发者_StackOverflow社区y help on this. Thanks


You made some mistakes.

Try changing the code to:

    link5Btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            jumbleMe(wordE.getText().toString());
        }
    });

and

private void jumbleMe(String word) {
    ArrayList<Character> al = new ArrayList<Character>();
    for (int i = 0; i < wordE.length(); i++) {
        al.add(word.charAt(i));
    }
    Collections.shuffle(al);
    String result = "";
    for (Character character : al) {
        result += character;
    }

    jumble.setText(result);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜