开发者

How to print the result of a method in android

I have a problem. I have a TextView, a TextEdit and a button:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final EditText TextEdit = (EditText) findViewById(R.id.TextEdit);  
    final TextView mix =(TextView) findViewById(R.id.mix);
    final Button button = (Button) findViewById(R.id.proces);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {


            // Perform action when clicking 



        }

and a method:

public String shuffle(String s) {  
            List<String> words = Arrays.asList(s.split(" "));  
            StringBuilder sb = new StringBuilder();  
 开发者_Go百科           for (String w : words) {  
                 w = mixMe(w);  
                sb.append(w);  
                sb.append(" ");  
            }  
            return sb.toString().trim();  
        }  

I want to take the text from the EditText field, apply to it the shuffle method and after that print it with setText, and all of this in the OnClik method, but I'm not sure how to do that. Can anyone help me please. Thank you


Try this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final EditText TextEdit = (EditText) findViewById(R.id.TextEdit);  
    final TextView mix =(TextView) findViewById(R.id.mix);
    final Button button = (Button) findViewById(R.id.proces);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {


            String s = shuffle(TextEdit.getText().toString());
            mix.setText(s);
        }


In your onClick method implement the following code:

public void onClick(View v) {
String suffledString = shuffle(TextEdit.getText().toString());
TextEdit.setText(suffledString);
        }

Also if its not necessary remove the final keywords as it might cause a problem later yet not tested. :)


something like

String s = shuffle(TextEdit.getText().toString());

TextEdit.setText(s);

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜