에 setOnClickListener 추가까지 내가 이해, 나는이 필요합니다 구성 요소의 레이아웃, 안드로이드 사용자 정의 구성 요소
- 하나의 XML 파일을;
- 하나의 자바 클래스.
실천을 위해 하나 만들어보세요. 두 개의 진행 표시 줄과 XML에서 하나의 버튼을 사용하고 클래스를 만들면 내 주요 활동에서 시도한 것보다 잘 작동합니다.
하지만 지금은 활동을 통해 내 버튼에서 OnClickListener를 설정하는 방법에 관심이 많습니다.이 부분에서는 실종 상태입니다.
내 loading_button.xml :
여기 LoadingButton.java<merge
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="4dp"
android:progress="0"
android:id="@+id/pg_top" />
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_ok"
android:background="@color/colorPrimary"
android:textColor="#ffffff"
android:layout_below="@+id/pg_top"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="5dp"
android:progress="0"
android:id="@+id/pg_bottom"
android:layout_below="@+id/btn_ok"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</merge>
:
public class LoadingButton extends RelativeLayout{
private ProgressBar pbTop;
private ProgressBar pbBottom;
private Button btnOk;
private int pbTopProgress = 0;
private int pbBottomProgress = 0;
public LoadingButton(Context context, AttributeSet attrs) {
super(context, attrs);
View view = View.inflate(context, R.layout.loading_button, this);
btnOk = (Button) view.findViewById(R.id.btn_ok);
pbTop = (ProgressBar) view.findViewById(R.id.pg_top);
pbBottom = (ProgressBar) view.findViewById(R.id.pg_bottom);
btnOk.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Doing stuff ok
}
});
}// LoadingButton
}
클래스 작품의 setOnClickListener,하지만 어떻게 내 주요 활동에서 설정할 수 있습니다?
MainActivity :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// My new component (it's id in activity_main.xml is 'lb')
LoadingButton lb = (LoadingButton) findViewById(R.id.lb);
// QUESTION - how to make something like this with btnOk:
lb.btnOkSetOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Do stuff
}
});
}
}
이 가능한 경우, 어떻게?
그렇지 않은 경우 맞춤 구성 요소를 만들고이 구성 요소의 특정 요소에 clickListener를 설정하는 올바른 방법은 무엇입니까?
내가 원했던 것! 감사. 하지만 당신은 setOnLoadingClickListener에서 약간의 실수를 저지른 것입니다 : public void setOnLoadingClickListener (OnLoadingButtonClickListener onLoadingClickListener) { mONOnLoadingButtonClickListener = onLoadingClickListener; } 두 번째 mONOnLoadingButtonClickListener를 onLoadingClickListener로 바꾸면 응답으로 표시됩니다. ^^ –
OK, 내 실수를 찾아 주셔서 감사합니다. – USKMobility