class MainActivity
package com.example.choi.ex08_dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View v){
Intent intent=null;
switch (v.getId()){
case R.id.button1:
intent=new Intent(this, DialogActivity.class);
break;
case R.id.button2:
intent=new Intent(this, AlertActivity.class);
break;
case R.id.button3:
intent=new Intent(this, SelectActivity.class);
break;
}
startActivity(intent);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.choi.ex08_dialog.MainActivity">
<Button
android:text="다이얼로그"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onClick" />
<Button
android:text="Alert 다이얼로그"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginTop="25dp"
android:id="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onClick"
/>
<Button
android:text="선택 다이얼로그"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:id="@+id/button3"
android:onClick="onClick"
/>
</RelativeLayout>
class DialogActivity
package com.example.choi.ex08_dialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class DialogActivity extends AppCompatActivity {
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
//액티비티 : 독립적인 화면
//다이얼로그 : 단독으로 화면을 띄울수 없음
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//다이얼로 객체 생성
Dialog dlg =new Dialog(DialogActivity.this);
dlg.setTitle("다이얼로그 연습"); //다이얼로그 제목
//코드로 레이아웃 작성
// TextView text =new TextView(DialogActivity.this);
// text.setText("다이얼로그의 내용");
// dlg.setContentView(text);
//xml로 레이아웃 작성
setContentView(R.layout.test);
dlg.show(); //다이얼로 표시
}
});
}
}
activity_dialog
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_dialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.choi.ex08_dialog.DialogActivity">
<Button
android:text="다이얼로그 열기"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:text="RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radioButton" />
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBar" />
<Switch
android:text="Switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/switch1" />
<RadioButton
android:text="RadioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radioButton2" />
</LinearLayout>
class AlertActivity
package com.example.choi.ex08_dialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class AlertActivity extends AppCompatActivity {
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alert);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//다이얼로그 생성
new AlertDialog.Builder(AlertActivity.this)
.setMessage("대화상자를 열었습니다.")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertActivity.this, "Yes버튼을 클릭 했습니다."
, Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertActivity.this, "No 버튼을 클릭 했습니다."
, Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Cancel 버튼을 클릭 했습니다."
, Toast.LENGTH_SHORT).show();
}
})
.show();
}
});
}
}
alert.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/alert"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.choi.ex08_dialog.AlertActivity">
<Button
android:text="다이얼로그 열기"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:id="@+id/button1" />
</RelativeLayout>
class SelectActivity
package com.example.choi.ex08_dialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class SelectActivity extends AppCompatActivity {
Button button1;
String[] items ={"coffee", "cocoa", "milk", "cola"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
// setItems(배열, 이벤트 핸들러)
@Override
public void onClick(View v) {
new AlertDialog.Builder(SelectActivity.this)
.setTitle("주문")
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(SelectActivity.this,
"주문한 메뉴 :"+items[which], Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("취소", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(SelectActivity.this,
"취소하였습니다.", Toast.LENGTH_SHORT).show();
}
})
.show();
}
});
}
}
select.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/select"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.choi.ex08_dialog.SelectActivity">
<Button
android:text="다이얼로그 열기"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/button1" />
</RelativeLayout>
댓글 ( 4)
댓글 남기기