Android 사용자 정의 로 딩 상자 의 코드 예제 구현
이 때 불 러 오 는 대화 상 자 를 사용자 정의 할 수 있 습 니 다.코드 는 다음 과 같 습 니 다.
public class LoadingDialog extends Dialog {
private static final int CHANGE_TITLE_WHAT = 1;
private static final int CHNAGE_TITLE_DELAYMILLIS = 300;
private static final int MAX_SUFFIX_NUMBER = 3;
private static final char SUFFIX = '.';
private ImageView iv_route;
private TextView detail_tv;
private TextView tv_point;
private RotateAnimation mAnim;
private Handler handler = new Handler() {
private int num = 0;
public void handleMessage(android.os.Message msg) {
if (msg.what == CHANGE_TITLE_WHAT) {
StringBuilder builder = new StringBuilder();
if (num >= MAX_SUFFIX_NUMBER) {
num = 0;
}
num++;
for (int i = 0; i < num; i++) {
builder.append(SUFFIX);
}
tv_point.setText(builder.toString());
if (isShowing()) {
handler.sendEmptyMessageDelayed(CHANGE_TITLE_WHAT, CHNAGE_TITLE_DELAYMILLIS);
}
else {
num = 0;
}
}
};
};
public LoadingDialog(Context context) {
super(context, R.style.Dialog_bocop);
init();
}
public LoadingDialog(Context context, boolean isTrans) {
super(context, isTrans ? R.style.Loading_Dialog_trans : R.style.Dialog_bocop);
init();
}
private void init() {
setContentView(R.layout.common_dialog_loading_layout);
iv_route = (ImageView) findViewById(R.id.iv_route);
detail_tv = (TextView) findViewById(R.id.detail_tv);
tv_point = (TextView) findViewById(R.id.tv_point);
initAnim();
getWindow().setWindowAnimations(R.anim.alpha_in);
}
private void initAnim() {
// mAnim = new RotateAnimation(360, 0, Animation.RESTART, 0.5f, Animation.RESTART, 0.5f);
mAnim = new RotateAnimation(0, 360, Animation.RESTART, 0.5f, Animation.RESTART, 0.5f);
mAnim.setDuration(2000);
mAnim.setRepeatCount(Animation.INFINITE);
mAnim.setRepeatMode(Animation.RESTART);
mAnim.setStartTime(Animation.START_ON_FIRST_FRAME);
}
@Override
public void show() {//
iv_route.startAnimation(mAnim);
handler.sendEmptyMessage(CHANGE_TITLE_WHAT);
super.show();
}
@Override
public void dismiss() {
mAnim.cancel();
super.dismiss();
}
@Override
public void setTitle(CharSequence title) {
if (TextUtils.isEmpty(title)) {
detail_tv.setText(" ");
}
else {
detail_tv.setText(title);
}
}
@Override
public void setTitle(int titleId) {
setTitle(getContext().getString(titleId));
}
public static void dismissDialog(LoadingDialog loadingDialog) {
if (null == loadingDialog) { return; }
loadingDialog.dismiss();
}
}
대응 하 는 구 조 는 다음 과 같다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_gravity="center"
android:background="@drawable/common_show_dialog"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:paddingTop="22dp"
android:gravity="center" >
<ImageView
android:id="@+id/iv_route"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/dialog_bocop_loading_rotate_anim_img" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="center_horizontal" >
<TextView
android:id="@+id/detail_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/tv_point"
android:ellipsize="marquee"
android:gravity="center"
android:singleLine="true"
android:text=" ..."
android:textColor="#ffffff"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_point"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="..."
android:textColor="#ffffff"
android:textSize="20sp" />
</RelativeLayout>
</LinearLayout>
예 를 들 어 Activity 에서 불 러 오기 대화 상자 호출 을 실현 하려 면:
LoadingDialog loadingDialog ;
if (null == loadingDialog) {
loadingDialog = new LoadingDialog(aty);
loadingDialog.setOnCancelListener(this);
}
loadingDialog.setTitle(“ ”);
if (!loadingDialog.isShowing()) loadingDialog.show();
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.