로그인 버튼을 누르면 로그인아이디, 비밀번호를 입력하는 팝업창이 뜨고, 입력 확인 하면 입력값을 Toast로 보여줌.

 

 - 로그인 버튼 선택                            - 로그인 팝업 창

 

 - 입력 후 로그인 버튼 클릭                 - 결과값 보여줌.

 

 

 

(1) activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="로그인" />

</LinearLayout>

 

 

(2) login.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="wrap_content"
    android:orientation="vertical"
    android:background="#00ffff">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="6.18" >
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="아이디" />
            <EditText
                android:id="@+id/edtId"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10" >
                <requestFocus />
            </EditText>
        </TableRow>
          <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="비밀번호" />
            <EditText
                android:id="@+id/edtPwd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10" >
                <requestFocus />
            </EditText>
        </TableRow>
</TableLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">
       <Button
            android:id="@+id/btnLogin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="로그인" />
        <Button
            android:id="@+id/btnCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="취소" />
    </LinearLayout>

</LinearLayout>

 

 

(3) MainActivity.java

package com.example.test27_popupdlg;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

 

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //로그인버튼 누르면 로그인창 띄우기
        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {           
            @Override
            public void onClick(View v) {
                //login.xml을 자바객체로 변환하기
                LinearLayout layout=
                   (LinearLayout)View.inflate(MainActivity.this,R.layout.login,null);
                //layout을 뷰로 갖는 팝업창만들기
                final PopupWindow popup=new PopupWindow(layout, 300,200,true);
                //화면 정가운데 팝업창 띄우기
                popup.showAtLocation(layout,Gravity.CENTER,0,0);
                Button btnLogin=(Button)layout.findViewById(R.id.btnLogin);
                Button btnCancel=(Button)layout.findViewById(R.id.btnCancel);
                final EditText edtId=(EditText)layout.findViewById(R.id.edtId);
                final EditText edtPwd=(EditText)layout.findViewById(R.id.edtPwd);
                //로그인버튼 눌렀을때
                btnLogin.setOnClickListener(new View.OnClickListener() {                   
                    @Override
                    public void onClick(View v) {
                        String msg=edtId.getText().toString() +"," +
                                   edtPwd.getText().toString();
                        Toast.makeText(MainActivity.this,msg,
                                Toast.LENGTH_SHORT).show();
                        //팝업창 닫기
                        popup.dismiss();
                    }
                });
                //취소버튼을 눌렀을때
                btnCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //팝업창 닫기
                        popup.dismiss();
                    }
                });
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

 

'Mobile > Android' 카테고리의 다른 글

#24 ( File )  (0) 2013.02.03
#23 ( Preference )  (0) 2013.02.03
#22 ( Activity LifeCycle )  (0) 2013.02.03
#21 ( Intent Action : 외부 프로그램과 연결 )  (0) 2013.02.02
#20 ( 다른 Activity 로 text와 이미지 넘기기 )  (0) 2013.02.02

+ Recent posts