본문 바로가기

Android

대화상자 - 커스텀대화상자

activity_main.xml

 

<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=".MainActivity" >

 

    <Button

        android:id="@+id/btn"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="다이얼 로그 보이기" />

 

</RelativeLayout>

 

custom.xml

 

 

<?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="vertical" >

 

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="제품" />

 

    <EditText

        android:id="@+id/product"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content" />

 

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="수량" />

 

    <EditText

        android:id="@+id/number"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:numeric="integer" />

 

    <CheckBox

        android:id="@+id/paymethod"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="착불결제" />

 

</LinearLayout>

 

 

 

MainActivity.java

 

package com.example.dialogex04;

 

 

import android.os.Bundle;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.Context;

import android.content.DialogInterface;

import android.view.Menu;

import android.view.View;

import android.view.inputmethod.InputMethodManager;

import android.widget.EditText;

import android.widget.LinearLayout;

import android.widget.Toast;

 

public class MainActivity extends Activity {

     InputMethodManager imm;

    

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

          

           imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

          

           findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {

               

                @Override

                public void onClick(View v) {

                     // TODO Auto-generated method stub

                     final LinearLayout linearLayout

                     = (LinearLayout)View.inflate(MainActivity.this, R.layout.cusom, null);

                    

                     new AlertDialog.Builder(MainActivity.this)

                     .setTitle("주문 정보를 입력하세요")

                     .setIcon(R.drawable.ic_launcher)

                     .setView(linearLayout)

                     .setPositiveButton("확인", new DialogInterface.OnClickListener() {

                          

                           @Override

                           public void onClick(DialogInterface dialog, int which) {

                                // TODO Auto-generated method stub

                                EditText product = (EditText)linearLayout.findViewById(R.id.product);

                                EditText number = (EditText)linearLayout.findViewById(R.id.number);

                               

                                imm.hideSoftInputFromWindow(product.getWindowToken(), 0);

                                imm.hideSoftInputFromWindow(number.getWindowToken(), 0);

                               

                                Toast.makeText(MainActivity.this, product.getText() + " 상품 " + number.getText() + "" , Toast.LENGTH_SHORT).show();

                           }

                     })

                     .show();

                }

           });

     }

 

     @Override

     public boolean onCreateOptionsMenu(Menu menu) {

           // Inflate the menu; this adds items to the action bar if it is present.

           getMenuInflater().inflate(R.menu.main, menu);

           return true;

     }

 

}





 

'Android' 카테고리의 다른 글

액티비티(Activity) - 액티비티간의 통신  (0) 2013.04.03
액티비티(activity) -  (0) 2013.04.03
대화상자 - MultiChoiceItems  (0) 2013.04.02
대화상자 - SingleChoiceItems  (0) 2013.04.02
대화상자 - SelectDialog  (0) 2013.04.02