본문 바로가기

Android

버튼과 에디트

<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"

    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" >

 

    <EditText

        android:id="@+id/et1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:inputType="text" />

 

    <Button

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="버튼 1"

        android:textColor="#0000ff"

        android:onClick="onClick" />

 

    <Button

        android:id="@+id/btn1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="버튼 2" />

 

</LinearLayout>

 

 

package com.example.testex03;

 

import android.os.Bundle;

 

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.view.inputmethod.InputMethodManager;

import android.widget.Button;

import android.widget.EditText;

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(INPUT_METHOD_SERVICE);

              

       

        Button btn1 = (Button)findViewById(R.id.btn1);

        btn1.setOnClickListener(new View.OnClickListener() {

               

                @Override

                public void onClick(View v) {

                     // TODO Auto-generated method stub

                     // alert

          

                     EditText et1 = (EditText)findViewById(R.id.et1);

                     String msg = et1.getText().toString();

                     Toast.makeText(MainActivity.this,msg, Toast.LENGTH_SHORT).show();

                                          //외부객체명

                    

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

                }

           });

    }

 

 

    @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;

    }

   

    public void onClick(View v){

      Toast.makeText(this, "버튼 클릭", Toast.LENGTH_SHORT).show();

    }

}