안드로이드 UI 다이얼로그 생성 1
프로그래밍/Android 2013. 8. 23. 02:43안드로이드 UI 다이얼로그 생성 1
다이얼로그의 종류
AlertDialog, ProgressDialog, DatePickerDialog, TimePickerDialog... 등
다이얼로그 생성
onCreateDialog(int) // 오픈 될때 최초 한번
onPrepareDialog( int , Dialog) // 오픈 될때마다
다이얼로그 닫기
dismiss()
dismissDialog(int) // from Activity
removeDialog(int) // Activity 에 보관되는 Dialog의 상태를 모두 제거 하면서 다이얼로그를 닫는다.
dismiss listener 사용하기
DialogInterface.OnDismissListener 인터페이스 정의
setOnDIsmissListener() 로 등록
※ cancel, 취소 시에서 dismiss listener 는 작동한다.
취소버튼에 의해 닫힌 경우
DialogInterface.OnCancelListener
setOnCancelListerner() 로 등록
Alert Dialog 생성
- Alert Dialog 에서는 positive, neutral, negative 3개의 버튼만을 가질 수 있다.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessagge("Are you sure you want to exit?") // 다이얼로그 메세지 정의
.setCancelable(false) // 다이얼로그가 취소될 수 없도록, 즉 사용자가 back 버튼을 눌러서 다이얼로그를 닫을 수 없도록 한다.
.setPositiveButton("Yes",
new DialogInterface.OnClickListener(){
public void onClick(DialogInterfafce dialog, int id){
MyActivity.this.finish();
}})
.setNegativeButton("No", new DialogInterface.OnClickListener(){
public void onClick( DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alert = builder.create();
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems( items, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int item){
Toast.makeText(getApplicationContext(), items( item),
Toast.LENGTH_SHORT).show();
}});
AlertDialog alert = builder.create();
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setSingleChoiceItems( items, -1, new DialogInterface.OnClickListener(){ // setItems => setSingleChoiceItems, selected 값 몇번째 아이템이 기본으로 선택되어져야 하는지를 나타낸다.
public void onClick(DialogInterface dialog, int item){
Toast.makeText(getApplicationContext(), items( item),
Toast.LENGTH_SHORT).show();
}});
AlertDialog alert = builder.create();
ProgressDialog dialog = ProgressDialog.show(
MyActivity.this, // 어플리케이션 컨텍스트
"", // 다이얼로그 타이틀
"Loading. Please wait...", // 메시지
true); // 진행상태의 무한 진행여부
true);
ProgressDialog progressDialog = new ProgressDialog( mContext); progressDialog.setProgressStyle( ProgressDialog.STYLE_HORIZONTAL); progressDialog.setMessage( "Loading..."); progressDialog.setCancelable( false);
public class NotificationTest extends Activity{
static final int PROGRESS_DIALOG = 0;
Button button;
ProgressThread progressThread;
ProgressDialog progressDialog;
public void onCreate( Bundle savedInstanceState){
super.onCreate( savedInstanceState);
setContentViewe( R.layout.main);
button = (Button)findViewById( R.id.progressDialog);
button.setOnClickListener( new OnClickListener(){
public void onClick( View v){
showDialog(PROGRESS_DIALOG);
}
}
}
protected Dialog onCreateDialog( int id){
switch( id){
case PROGRESS_DIALOG :
progressDialog = new ProgressDialog( NotificationText.this);
progressDialog.setProgressStyle( ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage( "Loading...");
progressThread = new ProgressThread( handler);
progressThread.start();
return progressDialog;
default :
return null;
}
final Handler handler = new Handler(){
public void handleMessage( Message msg){
int total = msg.getData().getInt("total");
progressDialog.setProgress(total);
if( total >= 100){
dismissDialog( PROGRESS_DIALOG);
progressThread.setState( ProgressThread.STATE_DONE);
}
}
};
private class ProgressThread extends Thread{
Handler mHandler;
final static int STATE_DONE = 0;
final static int STATE_RUNNING = 0;
int mState;
int total;
ProgressThread( Handler h){
mHandler = h;
}
public void run(){
mState = STATE_RUNNING;
total = 0;
while( mState == STATE_RUNNING){
try{
Thread.sleep(100);
}catch( InterruptedException e){
Log.e("ERROR", "Thread Interrupted");
}
Message msg = mHandler.obtainMessage();
Bundle b = new Bundle();
b.putInt("total", total);
msg.setData(b);
mHandler.sendMessage( msg);
total++;
}
}
public void setState(int state){
mState = state;
}
}
}
}
'프로그래밍 > Android' 카테고리의 다른 글
| 안드로이드 레이아웃 (0) | 2013.08.25 |
|---|---|
| 안드로이드 스타일과 테마 적용하기 (0) | 2013.08.25 |
| 안드로이드 노티피케이션 (0) | 2013.08.25 |
| Android 커스텀 다이얼로그 생성 (0) | 2013.08.23 |
| Android UI 메뉴 생성 (0) | 2013.08.23 |