通过单击按钮从EditText中发出混乱的字母

时间:2014-02-12 17:46:36

标签: java android

一般来说,它非常适合Android和编程。

只是想知道是否有人可以帮我在EditText中接受一个单词,然后混淆字母并显示它们?

我尝试了很多不同的事情,这个问题有些帮助How to jumble a word from EditText and apply the jumbled word into a TextView,但我仍然会收到强制关闭错误。

这是我的xml:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/jumble" 
    />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/Editbox1"
    android:layout_below="@+id/Editbox1"
    android:layout_marginTop="18dp"
    android:text="@string/edit_text" />

<EditText
    android:id="@+id/Editbox1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="21dp"
    android:ems="10"
    android:inputType="text"
    android:text="@string/enter_text" >
    </EditText>

和java:

package org.me.myandroidstuff;

import java.util.ArrayList;
import java.util.Collections;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;

public class JumbleTextApplicationActivity extends Activity 
  implements OnClickListener {

  public View TextView;
  public View EditText;
  public View Button;
  EditText editbox = (EditText)findViewById(R.id.Editbox1); 
  TextView jumbledword = (TextView) findViewById(R.id.jumble);
  Button btnJumble = (Button)findViewById( R.id.button );

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_jumble_text_application);
    TextView tbox1 =(TextView)findViewById(R.id.textBox1);
    tbox1.setTypeface(Typeface.SANS_SERIF);
    tbox1.setTypeface(Typeface.MONOSPACE);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.jumble_text_application, menu);
    return true;
  }

  @Override
  public void onClick(View v) {
    btnJumble.setOnClickListener( new View.OnClickListener(){
      public void onClick(View v){
        jumbleMe(editbox.getText().toString());
      }
    });
  }

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

2 个答案:

答案 0 :(得分:1)

这个问题正是你所需要的。只需为代码更改

即可
word = (EditText)findViewById(R.id.entry); 
jumble = (TextView) findViewById(R.id.jumble);
Button btnJumble = (Button)findViewById( R.id.button );

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

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

修改

试试这个

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/jumble"/>

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/Editbox1"
    android:layout_below="@+id/Editbox1"
    android:layout_marginTop="18dp"
    android:text="@string/edit_text" />

<EditText
    android:id="@+id/Editbox1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="21dp"
    android:ems="10"
    android:inputType="text"
    android:text="@string/enter_text" />

和java:

package org.me.myandroidstuff;

import java.util.ArrayList;
import java.util.Collections;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;

public class JumbleTextApplicationActivity extends Activity{

    public View TextView;
    public View EditText;
    public View Button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jumble_text_application);

        EditText editbox = (EditText)findViewById(R.id.Editbox1); 
        TextView jumbledword = (TextView) findViewById(R.id.jumble);
        Button btnJumble = (Button)findViewById( R.id.button );
        TextView tbox1 =(TextView)findViewById(R.id.textBox1);
        tbox1.setTypeface(Typeface.SANS_SERIF);
        tbox1.setTypeface(Typeface.MONOSPACE);

        btnJumble.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                jumbleMe(editbox.getText().toString());
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.jumble_text_application, menu);
        return true;
    }

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

答案 1 :(得分:1)

String text = yourTextView.getText().toString();
String[] words = text.split("\\s+");
List<String> wordList = Arrays.asList(words);
Collections.shuffle(wordList);
text = StringUtils.join(wordList, " ");
yourTextView.setText(text);

在点击监听器上的按钮内执行此操作以随机播放单词。对于forceclose提供logcat