woshidan's blog

あいとゆうきとITと、とっておきの話。

mixi Android Training 第三回分メモ

昨日フットサルをしたのですが、どしろうと過ぎてパスカットしてもボールがまっすぐ蹴れないので結局また取られるどうしようもなさがあります……。

mixiの人のまとめがこちら。 http://qiita.com/punchdrunker/items/4c069025680d1f0707d3

どうにもこうにもまとめ方が分からなかったので、とりあえず実習と課題についてメモ。ソースコードhttp://mixi-inc.github.io/AndroidTraining/fundamentals/2.03.application-resource-management.html#Format をお借りしています。

実習

String Resource

format(変数を用いるStringリソース)の利用にはActivityなどcontextを扱う場所で、

getString(R.id.string.xxx, $1, $2...)

とします。

EditTextで扱う可変文字列の長さは

// CharSequence s
s.length()

で取れます。

EditTextのViewの入力値の監視は

// onStart()
EditText text = (EditText) findViewById(R.id.TextInput);
text.addTextChangedListener(this);

// onStop()
EditText text = (EditText) findViewById(R.id.TextInput);
text.removeTextChangedListener(this);

// http://developer.android.com/reference/android/text/TextWatcher.html
@Override
public void afterTextChanged(Editable s) {}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

public void onTextChanged(CharSequence s, int start, int before, int count) {
}

Drawable Resource

四角形の塗りつぶしの中に円を描く = 四角形の上に小さめの円を描く => layer-list drawableの利用

Animation Resource

  1. animのxmlを書く
  2. アニメーションをさせるViewを用意
  3. アクティビティから適用する
    3.1 アニメーション用のViewを取得
    3.2 アニメーションのリソースを取得
    3.3 アニメーション開始
// 3のアクティビティ
public class AnimationPracticeActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animation_practice);

        // 3.1 アニメーション用のViewを取得
        View animationView = findViewById(R.id.animation_view);
        // 3.2 アニメーションのリソースを取得
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.practice);
        // 3.3 アニメーション開始
        animationView.startAnimation(animation);
    }
}

課題

String Resource

英語の複数形 => とりあえず複数形はpluralsリソースを使います。

pluralsリソースは以下のような感じでvalues/plurals.xmlに定義

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <plurals name="my_eggs">
        <item quantity="one">I have one egg.</item>
        <item quantity="other">I have %d eggs.</item>
    </plurals>
</resources>

Activityなどcontextを持つクラスから取り出す時は、

getResources().getQuantityString(R.plurals.my_eggs, 2, 2);

のような感じです。

Drawable Resource

ボタン等をfocus,pressなどの状態によって表示を変えたい場合は、 selectorリソースを設定します。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- ボタンが押されたとき -->
    <item android:state_pressed="true" <!-- state_xxx="true"で状態を指定  -->
          android:drawable="@drawable/square_circle_pressed"/>
    <!-- 表示する見た目はandroid:drawable属性で指定 -->

そして、設定したリソースをButtonなどのandroid:background属性で指定すればいいみたいです。