2014-02-12 27 views
-2

真的很新的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); 
    } 
} 
+1

請發佈您的代碼和logcat。 –

回答

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

謝謝,但它仍然強制關閉,我用我現在的代碼編輯了我的問題!對不起,我很痛苦,但我太無能了 – user2237335

+0

不得不改變事物的地方,但最終它的工作!非常感謝! :D現在只是失敗了這個模塊的其餘部分哈哈。再次感謝! :) – user2237335

+0

@ user2237335沒問題。只要接受幫助你的答案,問題就會結束 – BigT

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