如何成功制作一个逐字逐句颠倒句子的android程序。 [英] How to successfully make an android program that reverses sentences word for word.

查看:92
本文介绍了如何成功制作一个逐字逐句颠倒句子的android程序。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将这个Windows java程序合并到android构建中但我没有

能够成功实现这一点。程序逐字反转输入的字符串。我如何让android反转句子就像代码与windows一样



我尝试过:



我已经成功地在windows中使用它并且它工作得很好,但是试图把它放在android中很难。请帮帮我。



这是我的代码



I have been trying to incorporate this windows java program into android build but I have not
been able to successfully achieve that. The program reverses inputted string word for word. How do I get the android to reverse sentences just like the code does with the windows

What I have tried:

I have already successfully apllied it in windows and it worked perfectly, but trying to put
infuse it in android has been difficult. Please help me out with it.

Here is my code

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class SubActivity extends Activity 
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main2);
}

//From here is problem. Please help me in fixing it 
public void RevOnClick(View view)
{
EditText main2EditText1 =(EditText)findViewById(R.id.main2EditText1);
String sen = main2EditText1.getText().toString();
String[] senRev = sen.split("\\b");

for (int n = senRev.length - 1; n >= 0; n--) 
{

TextView main2TextView1 = (TextView)findViewById(R.id.main2TextView1);
main2TextView1.setText(senRev[n]);
}
}
}

推荐答案

通过将setText()放在for循环中,它将始终用后续迭代中的内容替换现有内容。那是错误的。正确的方法是在for循环中执行单词的反转,然后仅在for循环结束后在编辑文本控件中将最终反转的单词显示为字符串。试试这个:

By putting the setText() inside the for loop, it will always replace existing content with the one from subsequent iteration. That is the mistake. The correct way is to do the reversal of words in the for loop, then display the final reversed words as string in the edit text control only after the end of the for loop. Try this:
String sen = "Welcome to Code Project";

String[] senRev = sen.split("\\b");

StringBuilder builder=new StringBuilder();

for (int n = senRev.length - 1; n >= 0; n--)
{
    builder.append(senRev[n]);
}

TextView main2TextView1 = (TextView)findViewById(R.id.main2TextView1);
main2TextView1.setText(builder.toString());


这篇关于如何成功制作一个逐字逐句颠倒句子的android程序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆