char配列を、String変数に変換しまとめるには、
String.valueOf(char[] data)
を使います。
例えば、「DOWN」と表示する場合
private char[] charactor = new char[4];
private String moji;
charactor[0] = (char)(68); //D
charactor[1] = (char)(79); //O
charactor[2] = (char)(87); //W
charactor[3] = (char)(78); //N
moji = String.valueOf(charactor);
と書きます。
ブログ記事「文字コードを使って、文字を表示する」のサンプルを、String変数に変換してから表示するようにソースコードを変更してみました。
package com.example.android.sampletwo; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.Window; import android.view.WindowManager; import android.widget.TextView; public class SampleTwo extends Activity { private TextView aTextView; private char[] charactor = new char[4]; private String moji; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); } @Override public boolean onTouchEvent(MotionEvent event) { aTextView = (TextView) findViewById(R.id.myTextView); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: charactor[0] = (char)(68); //D charactor[1] = (char)(79); //O charactor[2] = (char)(87); //W charactor[3] = (char)(78); //N moji = String.valueOf(charactor); aTextView.setText(moji); break; case MotionEvent.ACTION_UP: charactor[0] = (char)(85); //U charactor[1] = (char)(80); //P charactor[2] = (char)(32); //スペース charactor[3] = (char)(32); //スペース moji = String.valueOf(charactor); aTextView.setText(moji); break; case MotionEvent.ACTION_MOVE: charactor[0] = (char)(77); //M charactor[1] = (char)(79); //O charactor[2] = (char)(86); //V charactor[3] = (char)(69); //E moji = String.valueOf(charactor); aTextView.setText(moji); break; case MotionEvent.ACTION_CANCEL: aTextView.setText("ACTION_CANCEL"); break; } return super.onTouchEvent(event); } }
コメント