반응형
package com.example.sampledelayed;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
Handler handler=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView =findViewById(R.id.textView);
Button button =findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
request();
}
});
}
private void request(){
String title ="원격요청";
String message="데이터를 요청 하시겠습니까?";
String titleButtonYes="예";
String titleButtonNo="아니요";
AlertDialog dialog =makeRequestDialog(title,message,titleButtonYes,titleButtonNo);
dialog.show();
textView.setText("대화상자 표시중..");
}
private AlertDialog makeRequestDialog(CharSequence title, CharSequence message, CharSequence titleButtonYes, CharSequence titleButtonNo) {
AlertDialog.Builder requestDialog = new AlertDialog.Builder(this);
requestDialog.setTitle(title);
requestDialog.setMessage(message);
requestDialog.setPositiveButton(titleButtonYes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
textView.setText("5초후에 결과표시됨");
handler.postDelayed(new Runnable() {
@Override
public void run() {
textView.setText("요청완료됨");
}
},5000);
}
});
requestDialog.setNegativeButton(titleButtonNo, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { }
});
return requestDialog.create();
}
}
반응형
'안드로이드' 카테고리의 다른 글
ConText (0) | 2021.02.05 |
---|---|
일정시간이후에 실행하기 (0) | 2021.02.03 |
스레드와 핸들러 이해하기 (0) | 2021.02.03 |
Intent란 (0) | 2021.01.27 |
activity 자동회전 (0) | 2021.01.26 |