728x90
화면에 팝업창 띄우고 싶을 때
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="다이얼로그 "/>
<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="테스트"/>
</LinearLayout>
MainActivity.java
package com.example.dialogexample;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btn_dialog;
TextView tv_result;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_dialog = (Button) findViewById(R.id.btn_dialog);
tv_result = (TextView) findViewById(R.id.tv_result);//id 찾아 주는 것임
btn_dialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder ad = new AlertDialog.Builder(MainActivity.this);
ad.setIcon(R.mipmap.ic_launcher); //dialog창에서 ImageView로 조그맣게 띄울 아이콘
ad.setTitle("제목");
ad.setMessage("홍드로이드는 존잘입니까?");
final EditText et = new EditText(MainActivity.this);
ad.setView(et);//dialog 안에 et라는 객체 add 해줘라. View로 추가를 해줘라
ad.setPositiveButton("확인", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String result = et.getText().toString();
tv_result.setText(result);
dialog.dismiss();//현재 dialog 닫아라
}
});
ad.setNegativeButton("취소", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
}
});
}
}728x90
'[Android Studio] (Java)' 카테고리의 다른 글
| [Android Studio] (Spinner 드롭 다운) (0) | 2023.08.29 |
|---|---|
| [Android Studio] (Service 백 그라운드 음악) (0) | 2023.08.28 |
| [Android Stuido] (Thread&Handler 사용법) (0) | 2023.08.28 |
| [Android Studio] (Log출력 및 주석 다는 법) (0) | 2023.08.27 |
| [Android Studio] (Fragment) (0) | 2023.08.27 |