質問

I need to flip the text typed in textView in android. Example:

Hello to olleH

I need to rotate all the text typed... someone told me to manually edit every string, but i can't do it as it is a textView and I do not know what the user types.

Here is my code (it is ActivityMain):

package com.LCPInt.reversealphabet;

import android.os.Bundle;
import android.app.Activity;

import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class ReverseMain extends Activity {

private static final String TAG = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reverse_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.reverse_main, menu);
    return true;
}
public void normal(View view){
    Log.e(TAG, "normale");
    switch (view.getId()) {

    case R.id.button1:
          setContentView(R.layout.normal);
          break;
    }
}
public void reverse(View view){
    Log.e(TAG, "reversed");
    switch (view.getId()) {

    case R.id.button2:
          setContentView(R.layout.reversed);
          break;
    }
    }
public void gen(View view){
    Log.e(TAG, "generate"); 
    Toast.makeText(this, "ciao", Toast.LENGTH_SHORT).show();

}
役に立ちましたか?

解決

StringBuffer stringBuffer = new StringBuffer("Hello");
String reversed = stringBuffer.reverse().toString();

Here the doc for reverse().

Edit:

String textViewText = textViewInstance.getText().toString();
StringBuffer stringBuffer = new StringBuffer(textViewText);
String reversed = stringBuffer.reverse().toString();

他のヒント

Not sure what your end goal is, but you might be looking for the android:textDirection attribute which is used to provide correct text direction for right-to-left languages (e.g. Arabic).

See the android:textDirection documentation.

You can set this attribute on any view, not just a TextView.

The other answers will help you reverse the text that is typed into a TextView. This method will actually allow the user to enter the string in a right-to-left fashion. Depends what you are after!

try this:

static String reverseMe(String s) {
   StringBuilder sb = new StringBuilder();
   for(int i = s.length() - 1; i >= 0; --i)
     sb.append(s.charAt(i));
   return sb.toString();
 }

use

new StringBuilder("hello").reverse().toString()
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top