将windows java程序合并到android build

时间:2016-12-23 17:48:22

标签: java android regex

我一直在尝试将这个Windows java程序合并到android构建中,但我还没能成功实现。程序逐字反转输入的字符串。如何使用android来反转句子,就像代码用windows做的那样

查看代码

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]);
    }
}
}

1 个答案:

答案 0 :(得分:0)

在for循环中,您将组件的文本设置为您当前正在迭代的字母。通过使用StringBuilder.reverse()反转String并仅将其传递给setText(),它应该可以正常工作:

public void RevOnClick(View view)
{
    EditText main2EditText1 =(EditText)findViewById(R.id.main2EditText1);
    String sen = main2EditText1.getText().toString();
    String[] senRev = sen.split("\\b");

    // use a StringBuilder to assemble the reversed sentence
    StringBuilder result = new StringBuilder();

    for (int n = senRev.length - 1; n >= 0; n--)  
    {       
        // append each word 
        result.append(senRev[n]);

        if (n > 0) {
            // if there are more words add a ' '
            result.append(" ");
        }
    }

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