프로그래밍[Univ]/Android

[Android] Toast, Dialog

Cloud Travel 2012. 1. 24. 13:51
* Toast
 - 디버깅 메세지를 확인하거나 사용자에게 간단한 메세지를 출력
 - 생성 : Toast.makeText(Context context, String message, int duration)
 - Method
  > public void setGravity(int gravity, int xOffset, int yOffset)
     // = Toast가 보이는 위치를 지정해준다.(x좌표 : -160~160 / y좌표 : -240~240)
  > public void setMargin(float horizontalMargin, float verticalMargin)
     // = 외부 여백을 지정하여 위치를 지정 
 - res/drawable 폴더에 디스플레이를 위한 XML을 지정하여 Toast를 꾸밀수 있다.
   shape tag를 root로 잡고 실시한다.

* Alert Dialog
 - 생성 Method를 생성하여 다이어로그를 setting한다.
 - (In Method)
   AlertDialog NAME = new AlertDialog.Builder(this)
      .setTitle("title")  // 제목 지정
      .setMessage("message") // 메세징 설정
      .setIcon(R.drawable.ICON) // 메세지 아이콘 설정
      .setPositiveButton("Yes", new DialogInterface.OnClickListener(){ //Action 정의 
      }) //.setNeutraButton, .setNagativeButton도 같은 방법으로 정의
      .create(); //생성
   return NAME;

* Test Source

실행화면



ToastDialogActivity.java 

package exam.toastalert;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ToastDialogActivity extends Activity {
    /** Called when the activity is first created. */
    private EditText xOffsetText;
    private EditText yOffsetText;
    private EditText result;
    private Button AlertBtn;
    private Button ToastBtn;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button ToastBtn = (Button)findViewById(R.id.toastBtn);
        Button AlertBtn = (Button)findViewById(R.id.alertBtn);
        
        ToastBtn.setOnClickListener(new OnClickListener(){
         public void onClick(View v){
         xOffsetText = (EditText)findViewById(R.id.xOffset);
         yOffsetText = (EditText)findViewById(R.id.yOffset);
         if ( xOffsetText.getText().toString().length() != 0 && yOffsetText.getText().toString().length() != 0 ){
         int xOffset = Integer.valueOf(xOffsetText.getText().toString());
         int yOffset = Integer.valueOf(yOffsetText.getText().toString());
         String str = "X좌표 : " + xOffset + "\nY좌표 : " + yOffset;
        
         Toast toast = Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT);
         toast.setGravity(Gravity.CENTER, xOffset, yOffset);
         toast.show();
         }else{
         Toast.makeText(getBaseContext(), "Input X,Y", Toast.LENGTH_SHORT).show();
         }
         }
        });
        
        AlertBtn.setOnClickListener(new OnClickListener(){
         public void onClick(View v){
         AlertDialog AlertBox = createAlertBox();
         AlertBox.show();
         }
        });
    }
    
    private AlertDialog createAlertBox(){
    
     result = (EditText)findViewById(R.id.result);
    
     AlertDialog newAlertBox = new AlertDialog.Builder(this)
     .setTitle("TITLE")
     .setMessage("This is Test AlertBox")
     .setIcon(R.drawable.ic_launcher)
    
     .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
result.setText("Click Yes. Click Button Number" + Integer.toString(which) );
}
})
.setNeutralButton("Cancle", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
result.setText("Click Cancle. Click Button Number" + Integer.toString(which) );
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
result.setText("Click No. Click Button Number" + Integer.toString(which) );
}
})
.create();
    
return newAlertBox;
    }
}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="X좌표"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            />
        <EditText 
            android:id="@+id/xOffset"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="X좌표 입력(-160~160)"
            />
    </LinearLayout>
    
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Y좌표"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            />
        <EditText 
            android:id="@+id/yOffset"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="Y좌표 입력(-240~240)"
            />
    </LinearLayout>
    
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button 
            android:id="@+id/toastBtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="ShowToast"/>
</LinearLayout>

<Button
   android:id="@+id/alertBtn"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="ShowAlert"
   />
<EditText 
   android:id="@+id/result"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content" 
   android:focusable="false"
   />
    
</LinearLayout>