5가지 alert창
; 1~5번 alert창 만들기.
(1) (2)
(3) (4) (5)
(1) activity_main.xml
; 1~5번 버튼을 만든다.
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/button1"
android:text="2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/button2"
android:text="3" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/button3"
android:text="4" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/button4"
android:text="5" />
</RelativeLayout>
(2) order.xml
; alert5번 창 layout을 만든다.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="제품명" />
<EditText
android:id="@+id/edtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="수량" />
<EditText
android:id="@+id/edtCnt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<CheckBox android:id="@+id/payMethod"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="착불결재"/>
</LinearLayout>
</LinearLayout>
(3) MainActivity.java
;
package com.example.test16_alertdlg;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btn1;
Button btn2;
Button btn3;
Button btn4;
Button btn5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.button1);
btn2=(Button)findViewById(R.id.button2);
btn3=(Button)findViewById(R.id.button3);
btn4=(Button)findViewById(R.id.button4);
btn5=(Button)findViewById(R.id.button5);
@Override
public void onClick(View v) {
/*AlertDialog.Builder dlg =
new AlertDialog.Builder(MainActivity.this);
//제목설정
dlg.setTitle("나의 대화상자");
//아이콘
dlg.setIcon(R.drawable.ic_launcher);
dlg.setMessage("안녕하세요.나의 대화상자에요.!");
//화면에 보이기
dlg.show();*/
//위에것을 줄여서 아래와 같이 사용.
new AlertDialog.Builder(MainActivity.this)
.setTitle("나의 대화상자")
.setIcon(R.drawable.ic_launcher)
.setMessage("안녕하세요.나의 대화상자에요.!")
.show();
}
});
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("나의 대화상자")
.setIcon(R.drawable.ic_launcher)
.setPositiveButton("확인", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "확인", Toast.LENGTH_LONG).show();
}
})//긍정의 버튼
.setNeutralButton("중립", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "중립", Toast.LENGTH_LONG).show();
}
})//부정의 버튼
.setNegativeButton("닫기", null)//부정의 버튼
.show();
}
});
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.ic_launcher)
.setTitle("음식메뉴")
//res에 있는 string배열 얻어와 항목으로 보이기.
.setItems(R.array.foods, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {//선택한 인덱스 번호 which
//리소스 배열을 string배열로 얻어오기
String foods[]=getResources().getStringArray(R.array.foods);
//선택한 인덱스가 which에 담겨있음.
String selectItem = foods[which];//선택한 메뉴얻어오기.
Toast.makeText(MainActivity.this, "선택메뉴:" + selectItem, Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("닫기", null)
.show();
}
});
final boolean checked[]={false,false,false,false};
@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("메뉴선택")
.setIcon(R.drawable.ic_launcher)
.setMultiChoiceItems(R.array.foods,
checked,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
//체크된 상태를 checked배열에 저장.
checked[which]=isChecked;
}
}).setPositiveButton("확인",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// xml에 있는 문자열을 String배열로 바꾸기
String foods[] = getResources().getStringArray(R.array.foods);
String selected="";
for(int i=0;i<foods.length;i++){
//체크된 항목인 경우
if(checked[i]){
//문자열에 연결
selected+=" " + foods[i];
}
}
Toast.makeText(MainActivity.this, "선택메뉴:" + selected, Toast.LENGTH_LONG).show();
}
}).show();
}
});
//커스텀 대화상자 띄우기
btn5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//order.xml을 자바객체로 변환하기.
//LayoutInflater객체 얻어오기
final LinearLayout layout = (LinearLayout)View.inflate(MainActivity.this, R.layout.order, null);
new AlertDialog.Builder(MainActivity.this)
.setTitle("주문정보입력")
.setIcon(R.drawable.ic_launcher)
.setView(layout)
.setPositiveButton("확인", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 입력된 값 얻어오기
EditText edtName = (EditText)layout.findViewById(R.id.edtName);
EditText edtCnt = (EditText)layout.findViewById(R.id.edtCnt);
CheckBox payMethod = (CheckBox)layout.findViewById(R.id.payMethod);
String msg="주문정보:" + edtName.getText().toString() + "\n"
+ "수량 :" + edtCnt.getText().toString() + "\n"
+ (payMethod.isChecked()?"착불결제":"");
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("취소", null)
.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
'Mobile > Android' 카테고리의 다른 글
#18 (Intent 1: 화면 이동) (0) | 2013.02.02 |
---|---|
#17 (menu) (0) | 2013.02.02 |
#15 ( spinner ) (0) | 2013.02.02 |
#14 (ListView : layout파일 붙이고, 이벤트 주기 ) (0) | 2013.02.02 |
#13 (ListView: 만든 layout을 ListView에 붙이기) (0) | 2013.02.02 |