본문 바로가기

IT프로그래밍

안드로이드 버튼 리스너(OnClickListener), 폰트 설정

간단하게 EditText에 쓴 내용을 Button을 누르면 TextView에 가져와서 출력해주는 간단한 안드로이드 프로그램입니다.


우선 소스코드 입니다.


SRC파일

package com.example.button_event_0718;


import android.app.Activity;

import android.graphics.Typeface;

import android.os.Bundle;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;


public class MainActivity extends Activity {


EditText edit;                //에디트 텍스트 선언

TextView textview;        //텍스트 뷰 선언

Button button;              //버튼 선언

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);


//선언만 되어 있는 각각의 뷰 들을 findViewById(R.id.뷰의 ID)를 이용하여 정의 합니다

//이때 뷰의 ID는 아래의 레이아웃 파일(XML파일)에서 정의 합니다.


edit = (EditText) findViewById(R.id.edittext1);        //에디트 텍스트를 정의

textview = (TextView) findViewById(R.id.textview1);    //텍스트 뷰 정의

button =  (Button)findViewById(R.id.button1);            //버튼 정의

button.setOnClickListener(new View.OnClickListener() {        // 버튼이 눌러졌을 때 할 행동을 정의

@Override

public void onClick(View v) {

// TODO Auto-generated method stub


/*

폰트 설정

*/

Typeface face = Typeface.createFromAsset(getAssets(), "fonts/aa.otf");    

/*

텍스트 뷰의 텍스트를 set하겠다. 즉 설정 하겠다. 무엇으로 ? -> edit에서 text를 get 해와서 즉 얻어와서

*/

textview.setText(edit.getText());        //텍스트 뷰의 텍스트를 set, 즉 설정


/*

텍스트뷰의 Typeface, 즉 폰트를 face로 하겠다 face는 아까 위에서

aa.otf라는 폰트파일에서 받아온 폰트 입니다. 자세한 설명은 밑에

*/

textview.setTypeface(face);

}

});

}



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

}


}




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


    <EditText

         android:id="@+id/edittext1"

        android:layout_height="50dp"

        android:layout_width="match_parent"

        android:background="#888888"

        />

    

    <Button

        android:id="@+id/button1"

        android:text="누르세요"

        android:layout_height="50dp"

        android:layout_width="match_parent"

        android:layout_marginTop="50dp"

        ></Button>

    

    <TextView

        android:id="@+id/textview1"







        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerVertical="true"

        android:textSize="30dp"

        android:text="NULL" ></TextView>


</RelativeLayout>

위의 레이아웃 으로 그려진 화면입니다.

각 뷰마다 android:id= 어쩌구 라고 있는데 그곳에 id 를 넣고

소스코드에서 그 id를 찾아서 정의 해줍니다.

EditText의 배경은 구분되게 회색으로 하였습니다. android:background="#888888"



폰트 설정
우선 폰트파일을 위에 보이시는 assets에 fonts에 옮겨 복사합니다. 그리고 소스코드에서 보신 
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/aa.otf")
과 같이 설정합니다. aa.otf는 제가 임의로 저장한것이고 바꾸셔도 됩니다.
그리고 폰트를 적용하고 싶은곳에 setTypeface를 이용하여 적용합니다.


EditText에 글을 입력하고 버튼(누르세요)을 누르면 TextView에 폰트가 적용되어 입력했던 글들이 출력됩니다.

이상입니다~