저는 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
모든 로직은 뷰 클래스가 아닌 Activity 클래스에 있어야합니다. 레이아웃에서 텍스트를 보거나 클릭 리스너를 추가하려는 경우 레이아웃 요소를 ID로 액세스 할 수 있습니다. 메모장 튜토리얼 (http://developer.android.com/guide/tutorials/notepad/index.html)과 같은 완벽한 예제 중 하나를 통해 모든 것이 함께 어울리는 지 확인하는 데 도움이 될 수 있습니다. –
사용자 정의 뷰에 고유 한 추가 속성이 필요한 경우 올바른 xmlns를 추가 할 수있는 올바른 방법입니다. 이 프로젝트의 패널 위젯에는 자신의 속성을 추가하는 방법의 예가 있습니다. http://code.google.com/p/android-misc-widgets/ findViewById를 사용하여 활동의 사용자 정의보기에 대한 참조를 얻고 수동으로 추가 속성을 설정할 수도 있습니다. 두 가지 해결책 모두 잘못되었습니다. – schwiz