Custom AlertDialog 구현하기.
CustomAlertDialog를 구현해 보았다.
AlertDIalog에 들어가는 버튼은 addButton 함수를 이용해서 계속 추가시킬 수 있도록 하였다.
즉, Activity마다 각자 AlertDialog를 구현하지 않고, CustomAlertDialog 를 계속해서 사용할 수 있는 것이다.
이때 CustomAlertDialog에는 interface를 구현해 두어 각각의 Activity에서 각자의 Button Control이 가능하도록 하였다.
interface를 사용하는 방법에 대해서는 두개의 Activity가 다른 방식을 사용해 보았다.
아래 예제가 있다^^
*.AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="lee.hyeontae.AlertDialogEx"
android:versionCode="1"
android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon"
android:label="@string/app_name"> <activity android:name=".AlertDialogExActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".secondActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.second_layout" /> </intent-filter> </activity> </application> </manifest>
|
*. CustomAlertDialog.java
package lee.hyeontae.AlertDialogEx; import java.util.ArrayList; import android.app.Dialog; import android.content.Context; import android.graphics.Color; import android.util.Log; import android.view.Gravity; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; import android.widget.TextView; public class CustomAlertDialog extends Dialog implements
android.view.View.OnClickListener{ public final static int BUTTONTYPE_BLUETYPE = 0x0001; public final static int BUTTONTYPE_GRAYTYPE = 0x0002; private Context mContext = null; private onClickCustomAlertButton mListener = null; public EditText id_editText; public EditText pass_editText; public TextView textView = null; private LinearLayout buttonBaseView = null; private ArrayList<Button> buttonArray = null; private String message = null; public CustomAlertDialog(Context context, String message,
onClickCustomAlertButton listener) { super(context); setContentView(R.layout.alert_layout); mContext = context.getApplicationContext(); this.message=message; this.buttonArray = new ArrayList<Button>(); textView = (TextView)findViewById(R.id.alertMessage); buttonBaseView = (LinearLayout)findViewById(R.id.buttonBaseView); mListener = listener; id_editText = (EditText)findViewById(R.id.id_editText); pass_editText = (EditText)findViewById(R.id.pass_EditText); textView.setText(this.message); getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT); } @Override public void onClick(View v) { Button btn = (Button)v; Log.d("dodo4989","onClick"); if(mListener != null) { mListener.onClickAlertButton(buttonArray.indexOf(btn), btn); } if(id_editText.getVisibility()==0){ Log.d("dodo4989","TRUE"); } else { Log.d("dodo4989","FALSE"); this.dismiss(); } } @Override public boolean dispatchKeyEvent(KeyEvent event) { if(event.getAction() == KeyEvent.ACTION_UP &&
event.getKeyCode() == KeyEvent.KEYCODE_BACK) return false; return super.dispatchKeyEvent(event); } public Button addButton(String title, int buttonType) { Button tmpBtn = new Button(mContext); tmpBtn.setBackgroundResource(buttonType==BUTTONTYPE_BLUETYPE?R.drawable.alert_button_type_1 : R.drawable.alert_button_type_2); tmpBtn.setGravity(Gravity.CENTER); tmpBtn.setText(title); tmpBtn.setPadding(0, 0, 0, 0); buttonBaseView.addView(tmpBtn); tmpBtn.setOnClickListener(this); LayoutParams lp =
(LayoutParams)tmpBtn.getLayoutParams(); lp.weight=1; lp.height=LayoutParams.WRAP_CONTENT; lp.width=LayoutParams.FILL_PARENT; lp.leftMargin=5; lp.rightMargin=5; tmpBtn.setLayoutParams(lp); buttonBaseView.invalidate(); buttonArray.add(tmpBtn); return tmpBtn; } public interface onClickCustomAlertButton { public void onClickAlertButton(int buttonIndex, Button
buttonView); } }
|
*. alert_layout.xml
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/alert_bg" android:padding="20dip" android:orientation="vertical"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dip" android:paddingRight="5dip" android:orientation="vertical"> <TextView android:id="@+id/alertMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="false" android:textColor="#ffffff" android:layout_marginBottom="10dip" android:textSize="20sp" /> </LinearLayout> <EditText android:id="@+id/id_editText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:lines="1" android:hint="ID(Email)" android:visibility="gone" /> <EditText android:id="@+id/pass_EditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:lines="1" android:hint="Password" android:visibility="gone" android:layout_marginBottom="10dip" /> </LinearLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"> <LinearLayout android:id="@+id/buttonBaseView" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerVertical="true"></LinearLayout> </RelativeLayout> </LinearLayout>
|
*. AlertDialogExActivity.java
package lee.hyeontae.AlertDialogEx; import
lee.hyeontae.AlertDialogEx.CustomAlertDialog.onClickCustomAlertButton; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class AlertDialogExActivity extends Activity implements
onClickCustomAlertButton{ Button btn = null; @Override public void onCreate(Bundle
savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.d("dodo4989","first onCreate"); CustomAlertDialog alert = new
CustomAlertDialog( this, this.getResources().getString(R.string.offline_start), this ); alert.getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT); alert.addButton("확인", CustomAlertDialog.BUTTONTYPE_BLUETYPE); alert.addButton("취소", CustomAlertDialog.BUTTONTYPE_GRAYTYPE); alert.show(); btn = (Button)findViewById(R.id.nextActivity); btn.setOnClickListener(new View.OnClickListener()
{ @Override public void onClick(View v) { Log.d("dodo4989","first onClick"); Intent intent = new
Intent(AlertDialogExActivity.this, secondActivity.class); startActivity(intent); finish(); } }); } @Override public void onClickAlertButton(int buttonIndex, Button
buttonView) { switch (buttonIndex) { case 0: Log.d("dodo4989","0 CLICK"); break; case 1: Log.d("dodo4989","1 CLICK"); break; } } }
|
*. main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/nextActivity" android:text="NextActivity" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
|
*. secondActivity.java
package lee.hyeontae.AlertDialogEx; import
lee.hyeontae.AlertDialogEx.CustomAlertDialog.onClickCustomAlertButton; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class secondActivity extends Activity{ Button btn = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second_layout); Log.d("dodo4989","second
onCreate"); CustomAlertDialog alertDialog = new
CustomAlertDialog(this, this.getResources().getString(R.string.offline_start),new
onClickCustomAlertButton(){ @Override public void onClickAlertButton(int buttonIndex, Button
buttonView) { switch(buttonIndex){ case 0: Log.d("dodo4989","second onClick 0"); break; case 1: Log.d("dodo4989","second onClick 1"); break; case 2: Log.d("dodo4989","second onClick 2"); break; } } }); alertDialog.getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT); alertDialog.addButton("두번째 버튼 1",
CustomAlertDialog.BUTTONTYPE_BLUETYPE); alertDialog.addButton("두번째 버튼 2",
CustomAlertDialog.BUTTONTYPE_BLUETYPE); alertDialog.addButton("두번째 버튼 3",
CustomAlertDialog.BUTTONTYPE_GRAYTYPE); alertDialog.show(); btn = (Button)findViewById(R.id.prevActivity); btn.setOnClickListener(new View.OnClickListener()
{ @Override public void onClick(View v) { Log.d("dodo4989","second onClick"); Intent intent = new
Intent(secondActivity.this, AlertDialogExActivity.class); startActivity(intent); finish(); } }); } }
|
*.second_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/prevActivity" android:text="PrevActivity" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
|