私はAndroid開発を初めて使いました。 TextView、EditText、およびSkillDiceButton(Buttonクラスの拡張機能)のコンポジットビュー(SkillDiceGroup)を作成したいとします。私はそれが私のSkillDiceGroupを宣言するとき、純粋なコードとして働いて、そして私のXMLレイアウトでこれを入れてありますAndroid XMLコンポジットビュー
<com.jeremybush.d20.SkillDiceGroup android:id="@+id/skillDiceTest"
android:title="Foobar!"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.jeremybush.d20.SkillDiceGroup>
そして、私はこのコードを持っている:
public class SkillDiceGroup extends LinearLayout
{
// The View components
private TextView mTitle;
private EditText mSkill;
private SkillDiceButton mDice;
public SkillDiceGroup(Context context, AttributeSet attrs)
{
super(context);
this.setOrientation(HORIZONTAL);
mTitle = new TextView(context);
mTitle.setText("foobar");
addView(mTitle, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT
));
mSkill = new EditText(context);
addView(mSkill, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT
));
mDice = new SkillDiceButton(context, attrs);
mDice.setText("d20");
addView(mDice, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT
));
}
private class SkillDiceButton extends DiceButton
{
public SkillDiceButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void onClick(View view)
{
modifier = Integer.parseInt(mSkill.getText().toString());
super.onClick(view);
}
}
}
これは私がそれをしたいどのように動作しますが、私は希望3つの項目をxmlビューで独自に宣言するのが好きです。これどうやってするの?
私はドキュメントを読んでいますが、この例は全く同じではありません。私のコードの最後には、mSkill変数を参照する修飾子の割り当てがあります。私が必要とする論理のために、あなたの例は直接の翻訳ではないと思います。私は間違っているかもしれませんが(そしておそらく午前)。 – zombor
ロジックは、ビュークラスではなくアクティビティクラスに配置する必要があります。レイアウトの要素にアクセスするには、idを使用してテキストを表示するか、クリックリスナーを追加する必要があります。メモ帳のチュートリアル(http://developer.android.com/guide/tutorials/notepad/index.html)のような完全な例の1つを使って、すべてがどのように適合すると思われるかを調べるのに役立ちます。 –
これは正しい方法です。カスタムビューに独自の属性が必要な場合は、独自のxmlnsも追加できます。このプロジェクトのパネルウィジェットには、独自の属性を追加する方法の例があります。 http://code.google.com/p/android-misc-widgets/ findViewByIdを使用して、アクティビティ内のカスタムビューへの参照を取得し、余分な属性を手動で設定することもできます。どちらの解決策も間違っていません。 – schwiz