개발언어/JAVA
자바 창끄기, alertdialog 만들기
우주먼지쪼가리
2020. 11. 18. 09:56
반응형
package AlertDialog.gohool.alertdialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private AlertDialog.Builder alertDialog;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 메인액티비티에서 알림 보여주기
alertDialog = new AlertDialog.Builder(MainActivity.this);
//제목
alertDialog.setTitle(getResources().getString(R.string.title));
//내용
alertDialog.setMessage(getResources().getString(R.string.message));
//창끄기 가능?
alertDialog.setCancelable(false);
//긍정 (yes를 누르면 어떻게 할거냐~)
alertDialog.setPositiveButton(getResources().getString(R.string.possitive), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MainActivity.this.finish();
}
});
//부정 (no를 누르면 어떻게 할거냐~)
alertDialog.setNegativeButton(getResources().getString(R.string.niggative), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
//본체 생성
AlertDialog dialog = alertDialog.create();
//보여줘 이젠!!!..
dialog.show();
}
});
}
}
alertDialoh는 다른 폼?과는 다르게 액티비티_메인.엑스엠엘에서 만들지 않고,
.java에서 바로 생성을 하여 사용 할 수 있다.
보이는 것과는 다르게 초보자가 만들기에는 복잡하진 않았다.
잼있었다.
만들며 설명을 위에 다 적어놨으니, 어느 부분에 어느것을 만드는지 참고 하길 바란답.
반응형