// DialogDemo.java
package com.example.hellow;
import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Toast;
public class DialogDemo extends Activity{ Button list, radio, check, dialog1, login, progress, datePickerFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_demo);
Button alert = (Button)findViewById(R.id.alert);
alert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new AlertDialog.Builder(DialogDemo.this) .setIcon(R.drawable.ic_launcher) .setMessage(R.string.dialog_message) .setTitle(R.string.dialog_title) .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { Toast.makeText(DialogDemo.this, "OK Button Click", Toast.LENGTH_SHORT).show(); } }) .setNegativeButton("NO", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { Toast.makeText(DialogDemo.this, "NO Button Click", Toast.LENGTH_SHORT).show(); } }) //.create() .show(); } });
list = (Button)findViewById(R.id.list);
list.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new AlertDialog.Builder(DialogDemo.this) .setTitle(R.string.pick_color) .setItems(R.array.color_array, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) {
switch(which){ case 0 : list.setBackgroundColor(Color.RED); break; case 1 : list.setBackgroundColor(Color.GREEN); break; case 2 : list.setBackgroundColor(Color.BLUE); break; } } }) .show(); } });
radio = (Button)findViewById(R.id.radio);
radio.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new AlertDialog.Builder(DialogDemo.this) .setTitle(R.string.pick_color) .setSingleChoiceItems(R.array.color_array, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) {
switch(which){ case 0 : radio.setBackgroundColor(Color.RED); break; case 1 : radio.setBackgroundColor(Color.GREEN); break; case 2 : radio.setBackgroundColor(Color.BLUE); break; } } }) .setNeutralButton("Close", null) .show(); } });
check = (Button)findViewById(R.id.check);
check.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new AlertDialog.Builder(DialogDemo.this) .setTitle(R.string.pick_color) .setMultiChoiceItems(R.array.color_array, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { String color=""; if(isChecked){ if(which == 0){ check.setBackgroundColor(Color.RED); color = "red"; } else if(which == 1){ check.setBackgroundColor(Color.GREEN); color = "green"; } else if(which == 2){ check.setBackgroundColor(Color.BLUE); color = "blue"; }
Toast.makeText(DialogDemo.this, "color is " + color + "!!!", Toast.LENGTH_SHORT).show(); } else { if(which <= 2){ check.setBackgroundColor(Color.GRAY); }
Toast.makeText(DialogDemo.this, "color unselected!!!", Toast.LENGTH_SHORT).show(); } } }) .setNeutralButton("Close", null) .show(); } });
dialog1 = (Button)findViewById(R.id.dialog1);
dialog1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FireMissilesDialogFragment fragment = new FireMissilesDialogFragment(); Bundle bundle = new Bundle(); fragment.setContext(DialogDemo.this); fragment.onCreateDialog(bundle).show(); } });
login = (Button)findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showAlertDialog(); } }); progress = (Button)findViewById(R.id.progress);
progress.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ProgressDialog dialog = new ProgressDialog(DialogDemo.this); dialog.setTitle("Indeterminage"); dialog.setMessage("Please wait while loading..."); dialog.setIndeterminate(true); dialog.setCancelable(true); dialog.show(); } }); datePickerFragment = (Button)findViewById(R.id.datePickerFragment);
datePickerFragment.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { DatePickerFragment fragment = new DatePickerFragment(); Bundle bundle = new Bundle(); fragment.setContext(DialogDemo.this); fragment.onCreateDialog(bundle).show(); } }); }
private void showAlertDialog(){ LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); LinearLayout loginLayout = (LinearLayout) vi.inflate(R.layout.logindialog, null);
final EditText id = (EditText) loginLayout.findViewById(R.id.id); final EditText pw = (EditText) loginLayout.findViewById(R.id.pw);
new AlertDialog.Builder(this).setTitle("로그인").setView(loginLayout) .setNeutralButton("확인", new DialogInterface.OnClickListener(){
@Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(DialogDemo.this, "ID : " + id.getText().toString() + "\nPW : " + pw.getText().toString(), Toast.LENGTH_SHORT).show(); } }).show(); } }
|