티스토리 뷰
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/btnAlert"
android:layout_width="200dp"
android:layout_height="90dp"
android:text="Alert"
android:textSize="26dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
AlertDialog alertDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alertDialog = new AlertDialog.Builder(getApplicationContext())
.setTitle("Title")
.setMessage("Message")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Cancel", Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Neutral", Toast.LENGTH_SHORT).show();
}
})
.create();
Button btnAlert = findViewById(R.id.btnAlert);
btnAlert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.show();
}
});
}
}

MainActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
AlertDialog alertDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alertDialog = new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Message")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Cancel", Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Neutral", Toast.LENGTH_SHORT).show();
}
})
.create();
Button btnAlert = findViewById(R.id.btnAlert);
btnAlert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.show();
}
});
}
}
alertDialog = new AlertDialog.Builder(getApplicationContext()) 에서
alertDialog = new AlertDialog.Builder(this) 로 수정

'Android > Java' 카테고리의 다른 글
| [Java] MVVM (0) | 2020.07.23 |
|---|---|
| [Java] RecyclerView + ItemClickListener + ItemLongClickListener (0) | 2020.07.21 |
| [Java] Room (0) | 2020.07.20 |
| [Java] SQLite + Singleton Pattern (0) | 2020.07.18 |
| [Java] Intent (0) | 2020.07.18 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Android
- fragment
- ViewModel
- ArrayList
- Architecture Pattern
- 혀가 길지 않은 개발자
- James Kim
- recyclerview
- Vue.js #Vue.js + javascript
- ViewPager2
- Kotlin
- 안드로이드 #코틀린 #Android #Kotlin
- DataBinding
- XML
- 자바
- handler
- JSONObject
- 코틀린
- CoordinatorLayout
- activity
- Intent
- JSONArray
- java
- View
- TabLayout
- Livedata
- coroutine
- Design Pattern
- 안드로이드
- MVVM
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함