본문 바로가기

플밍 is 뭔들/안드로이드_인강

11-2. 팝업대화상자

※ CustomLayoutDialog
 - 커스텀 뷰는 레이아웃 xml 파일을 만들어서 안드로이드 기본 Alert Dialog 틀에 안의 형식만 바꿔준다.

※ 구현
MainActivity
public class MainActivity extends Activity {
      Button btn;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            btn = (Button) findViewById(R.id.bt_01);
            btn.setOnClickListener(listener);
                  
      }
      
      OnClickListener listener = new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                  // TODO Auto-generated method stub
                  
                  //Dialog 부분에 적용할 레이아웃을 세팅한다.
                  LinearLayout ll = (LinearLayout)View.inflate(MainActivity.this, R.layout.custom_dialog, null);
                  //텍스트를 입력받을 부분을 세팅
                  final EditText et = (EditText)ll.findViewById(R.id.ev_01);
                  new AlertDialog.Builder(MainActivity.this)
                        .setTitle("점심메뉴")
                        //AlertDialog와 다르게 setView가 생겼다... view 부분을 우리가 만든 레이아웃으로 바꾼다.
                        .setView(ll)
                        .setIcon(R.drawable.ic_launcher)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                                    // TODO Auto-generated method stub
                                    String msg = et.getText().toString();
                                    Toast.makeText(MainActivity.this, msg , Toast.LENGTH_SHORT).show();
                              }
                        })
                        .setNegativeButton("NO", new DialogInterface.OnClickListener(){
                              @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                          // TODO Auto-generated method stub
                                          Toast.makeText(MainActivity.this, "주문취소", Toast.LENGTH_SHORT).show();
                                    }
                        }).show();
            }
      };
}

activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidex2.MainActivity" >
   <Button
       android:id="@+id/bt_01"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:padding="30px"
       android:text="Custom Dialog"
       />
</RelativeLayout>

custom_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
   
    <ImageButton
        android:id="@+id/iv_01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        />
      
    <EditText
        android:id="@+id/et_01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="input message"
        />
</LinearLayout>





'플밍 is 뭔들 > 안드로이드_인강' 카테고리의 다른 글

12-1. 노티(Notification)바  (0) 2016.11.27
11-3. 팝업대화상자  (0) 2016.11.27
11-1. 팝업대화상자  (0) 2016.11.27
10-1. 액션바 & 옵션메뉴  (0) 2016.11.27
09-1. 프레그먼트  (0) 2016.11.27