最初の例で、Viewの継承クラスである
TextView、EditText、Button、の部品を利用しました。
この時は、レイアウト用の activity_main.xml で各Viewの部品を配置しました。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
・・・・・省略・・・・ />
<TextView
android:id="@+id/textView1"
・・・・・省略・・・・ />
<EditText
android:id="@+id/editText1"
・・・・・省略・・・・ />
<Button
android:id="@+id/button1"
・・・・・省略・・・・ />
<TextView
android:id=@+id/"@+id/textView2"
・・・・・省略・・・・ />
</RelativeLayout>
そして、MainActivityのonCreateメソッドの中で、
setContentView(R.layout.activity_main);により生成して画面に見えるようにしています。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
つまり、setContentViewメソッドの中で
各種のView を生成するコンストラクタが実行されて
それを使うActivityのインスタンスで管理されている仕組みになっています。
これ以降において、activity_main.xmlの中で指定したidから次の
findViewByIdメソッドを使う表現で、
指定のViewのインスタンスを得ることができます。
(
R,id.textView1 はレイアウトリソースでidを使うと自動反映する
int型フィールドです。)
View view = this.findViewById(R.id.textView1);
このように、
TextViewと、EditView、Buttonなどの視覚的に見える部品のスーパークラスが、
Viewというわけです。
(実はRelativeLayoutもViewのサブクラスです。)
しかしこの取得の方法では、サブクラスが持つ独自の情報やメソッドや直接表現できないので、
次のようにキャストして使います。
それによって例えば setTextで表示文字列変更ができるようになります。
TextView txtview = (TextView )this.findViewById(R.id.textView1);
txtview.setText("変更後の文字列");
一般的は、後でプログラムで扱うViewをActivityを継承したクラスのインスタンス変数で管理し、
各インスタンスメソッドから扱えるようにします。
次のように setContentViewメソッドを使わないで、
各Viewのコンストラクタで配置する方法もあります。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);//activity_main.xmlのレイアウトは使わない
android.widget.RelativeLayout layout = new android.widget.RelativeLayout(this);
setContentView(layout);
android.widget.RelativeLayout.LayoutParams params;
android.widget.TextView textView1 = new android.widget.TextView(this);
textView1.setId( 9999 );// idを付けないと、RelativeLayoutのの相対対象にできない。
textView1.setText("Hello world!");
layout.addView(textView1);
params = new android.widget.RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
textView1.setLayoutParams(params);//部品配置
android.view.View view = this.findViewById( 9999 );
android.util.Log.d("確認",(view == textView1)+"");//trueを表示する。
}
上記の最後で、findViewByIdメソッドでID検索で得られたView が生成したTextViewと一致するか検証しています。
上記で紹介した TextView、EditText、Buttonのクラス構造を次に示します。
java.lang.Object
android.view.View
android.widget.TextView
android.widget.EditText
android.widget.Button
よく使いそうな、android.view.View のメソッドいくつか紹介します。
view.setLayoutParams(params)
view.getWidth()
view.getHeight()
view.setBackgroundColor(int olor)
view.setAlpha(float alpha)
view.setBackgroundResource(int resid)
view.setBackgroundDrawable(Drawable obj)
view.setFocusable(boolean focusable)
view.getContext()
view.getParent()
view.setOnClickListener(ClickListener obj)
view.setOnTouchListener(OnTouchListener)
よく使いそうな、android.widget.TextView のメソッドいくつか紹介する。
setText(CharSequence text)
append(CharSequence text)
setSingleLine(boolean singleLine)
textView1.getGravity();
textView1.setGravity(android.view.Gravity.CENTER);
setEms(int size)
setTextSize(int,float)
setTextScaleX(float)
setTextColor(int)
setTypeface(Typeface tf, int style)
setPadding(int left, int top, int right, int bottom)
getSelectionEnd()
getSelectionStart()
上記の
setTypefaceで、スタイルとファミリーを指定できます。
この第一引数のフォントファミリー(android.graphics.Typeface tf)の値は、次の種類があります。
Typeface.DEFAULT |
Typeface.DEFAULT_BOLD |
Typeface.SERIF |
Typeface.SANS_SERIF |
Typeface.MONOSPACE |
また、setTypefaceの第二引数は、フォントスタイル(int style)の値で次の指定ができます。
Typeface.NORMAL |
Typeface.BOLD |
Typeface.ITALIC |
Typeface.BOLD_ITALIC |
よく使いそうな、android.widget.EditText のメソッドいくつか紹介する。
EditText.setFillters(InputFillter filters[]);//入力最大文字数などの入力制限
EditText.setInputType(int type);//文字や数値などの入力種類の指定
Editable getText()
selectAll()
setSelection(int start, int stop)
void EditText.setHorizontallyScrolling(boolean showFlag);//falseの引数で、収まらない文字は自動改行で表示する。
なお、上記で示したように、ViewでsetOnClickListenerメソッドでイベント処理用インスタンスを指定できます。
つまり、android.widget.Buttonでなくてもクリック操作の処理がすべてのViewで使えます。
Buttonを使うメリットは、「ボタンの動作の画面移り変わり(アニメーション)」などが既に備わっていることでしょう。