public class DialogT extends Activity {
private Button b1, b2, b3, b4, b5, b6, b7;
private LinearLayout ll;
private EditText et, et2;
private TextView tv;
boolean flag = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button)findViewById(R.id.Button01);
b2 = (Button)findViewById(R.id.Button02);
b3 = (Button)findViewById(R.id.Button03);
b4 = (Button)findViewById(R.id.Button04);
b5 = (Button)findViewById(R.id.Button05);
b6 = (Button)findViewById(R.id.Button06);
b7 = (Button)findViewById(R.id.Button07);
et = (EditText)findViewById(R.id.EditText01);
et2 = (EditText)findViewById(R.id.EditText02); // 받는 메세지
tv = (TextView)findViewById(R.id.TextView01);
tv.setText("Italic, highlighted, bold.", TextView.BufferType.SPANNABLE);
tv.setOnClickListener(new View.OnClickListener() {
int endleng = tv.getText().length();
int startleng = 0;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//tv.setBackgroundColor(Color.BLACK);
//tv.setHighlightColor(color.background_dark);
//tv.setLinkTextColor(color.background_dark);
if(flag==false){
tv.setHighlightColor(Color.YELLOW);
Spannable str = (Spannable)tv.getText();
str.setSpan(new BackgroundColorSpan(0xFFFFFF00),
0, endleng, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new ForegroundColorSpan(0xFFFFFF00), 0, endleng,
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
tv.setText(str);
flag = true;
}else{
Spannable str = (Spannable)tv.getText();
str.setSpan(new BackgroundColorSpan(Color.BLACK),
0, endleng, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(str);
flag = false;
}
}
});
ll = (LinearLayout)findViewById(R.id.LinearLayout01);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.Button01: d1(); break;
case R.id.Button02: d2(); break;
case R.id.Button03: d3();break;
case R.id.Button04: d4();break;
case R.id.Button05: d5();break;
case R.id.Button06: d6();break;
case R.id.Button07: d7();break;
}
}
};
b1.setOnClickListener(listener);
b2.setOnClickListener(listener);
b3.setOnClickListener(listener);
b4.setOnClickListener(listener);
b5.setOnClickListener(listener);
b6.setOnClickListener(listener);
b7.setOnClickListener(listener);
}
private View v;
private EditText cet1,cet2,cet3,cet4;
private Button cb1,cb2,cb3,cb4;
private void initCustom(){
//v = View.inflate(DialogT.this, R.layout.custom, (ViewGroup)findViewById(R.id.LinearLayout02));
v = View.inflate(this, R.layout.custom, null);
cet1 = (EditText)v.findViewById(R.id.EditTextD01);
cet2 = (EditText)v.findViewById(R.id.EditTextD02);
cet3 = (EditText)v.findViewById(R.id.EditTextD03);
cet4 = (EditText)v.findViewById(R.id.EditTextD04);
cb1 = (Button)v.findViewById(R.id.ButtonD01);
cb2 = (Button)v.findViewById(R.id.ButtonD02);
cb3 = (Button)v.findViewById(R.id.ButtonD03);
cb4 = (Button)v.findViewById(R.id.ButtonD04);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.ButtonD01:
et2.setText(cet1.getText().toString());
break;
case R.id.ButtonD02:
et2.setText(cet2.getText().toString());
break;
case R.id.ButtonD03:
et2.setText(cet3.getText().toString());
break;
case R.id.ButtonD04:
et2.setText(cet4.getText().toString());
break;
}
}
};
cb1.setOnClickListener(listener);
cb2.setOnClickListener(listener);
cb3.setOnClickListener(listener);
cb4.setOnClickListener(listener);
}
private void d1(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.elephant);
builder.setTitle("주의");
builder.setMessage("졸지 맙시다.");
builder.show();
}
private void d2(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.elephant);
builder.setTitle("캠페인");
builder.setMessage("아침밥은 먹고 다닙시다.");
builder.setNeutralButton("확인", null);
builder.show();
}
private void d3(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.giraffe);
builder.setTitle("질의");
builder.setMessage("당신은 여성입니까?");
DialogInterface.OnClickListener listener2 = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showMsg("which : " + which);
if(which == -1){
showMsg("당신은 여성입니다.");
}else{
showMsg("당신은 남성입니다.");
}
}
};
builder.setPositiveButton("예", listener2);
builder.setNegativeButton("아니요", listener2);
builder.show();
}
private void d4(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.giraffe);
builder.setTitle("배경색");
builder.setMessage("배경색으로 좋아하는 색깔은?");
DialogInterface.OnClickListener listener2 = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showMsg("which : " + which);
if(which == -1){
b4.setBackgroundColor(Color.RED);
ll.setBackgroundColor(Color.CYAN);
}else if(which == -3){
b4.setBackgroundColor(Color.YELLOW);
ll.setBackgroundColor(Color.DKGRAY);
}else{
b4.setBackgroundColor(Color.BLUE);
ll.setBackgroundColor(Color.GREEN);
}
}
};
builder.setPositiveButton("빨강", listener2);
builder.setNeutralButton("노랑", listener2);
builder.setNegativeButton("파랑", listener2);
builder.show();
}
static final String JOBS[] = {"변리사","변호사","검사","판사","개발자",
"기획자","디자이너","의사","선생님","프로게이머"};
private void d5(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.guitar);
builder.setTitle("당신의 직업은?");
builder.setSingleChoiceItems(JOBS, 0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
// 직업을 바꾸세요.
showMsg("which : " + which);
et.setText("내 직업은"+ JOBS[which] + "입니다.");
}
});
builder.setNeutralButton("확인", null);
builder.show();
}
static final String HOBBYS[] = {"영화","음악","낚시","게임","골프", "수영"};
static final boolean flags[] = {false,false,false,false,false,false,};
String str="";
private void d6(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.mammoth);
builder.setTitle("당신의 취미는 무엇인가요?");
builder.setMultiChoiceItems(HOBBYS, flags,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
//showMsg("XX 선택됨");
//showMsg("XX 해제됨");
if(isChecked){
//str += HOBBYS[which] + " ";
showMsg("'" + HOBBYS[which] +"'선택됨");
}else{
showMsg("'" + HOBBYS[which] +"' 해제됨");
}
}
});
builder.setNeutralButton("확인", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
for(int i=0; i<HOBBYS.length; i++){
if(flags[i] == true)
str += HOBBYS[i] + " ";
}
showMsg("당신의 취미는 " + str + "입니다.");
}
});
builder.show();
}
private void d7(){
// 커스텀 다이얼로그를 띄우시오.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.mammoth);
builder.setTitle("커스텀 다이얼로그");
builder.setNeutralButton("확인", null);
builder.setNegativeButton("취소",null);
initCustom();
builder.setView(v);
builder.show();
}
private void showMsg(String msg){
Toast.makeText(this, msg, 0).show();
}
public DialogT() {
// TODO Auto-generated constructor stub
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.mammoth);
builder.setTitle("종료");
builder.setNeutralButton("확인", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});
builder.setNegativeButton("취소",null);
builder.setView(v);
builder.show();
}
return false;
}
}