프로그래밍 공부
작성일
2022. 9. 21. 15:30
작성자
WDmil
728x90

text 속성

  • 텍스트 뷰에 나타나는 문자열을 표현
  • android:text ="텍스트 입니다"

 

text Color 속성

  • 글자의 색상을 지정 #RRGGBB, #AARRGBB 형식
  • android:textColor ="#00FF00"

 

textSize 속성

  • 글자의 크기를 dp, px, in, mm ,sp 단위로 지정
  • android:textSize = "30dp"

 

typeface 속성

  • 글자의 글꼴을 지정 값으로 sans, serif, monospace를 설정 디폴트는 노말임
  • android:textface="serif"

 

textStyle 속성

  • 글자의 스타일을 지정 값으로 bold, italic, bold|italic 을 설정가능 디폴트는 노말임
  • android:textStyle="bold|italic"

 

singleLine속성

  • 글이 길어 줄이 넘어갈 경우 강제로 한줄까지만 출력하고 문자열 맨 뒤에 '...'을 출력 값으true 와 false를 설정할 수 있고 디폴트는 false
  • android:singleLine="true"

 

Java 코드로 XML 속성 설정

기본적인 텍스트 뷰만 만들어놓고 id속성과 text만 설정한 XML파일은 다음과 같다.

 

Java 코드를 다음과 같이 설정하여 화면에 적용이 가능하다.

 

XML 속성 관련 메소드 비고
background setBackgroundColor() View클래스
clickable setClickable() View클래스
focusable setFocusable() View클래스
id setId() View클래스
longClickable setLongClickable() View클래스
padding setPadding() View클래스
rotation setRotation() View클래스
scaleX, scaleY setScaleX(), setScaleY() View클래스
visibility setVisibility() View클래스
gravity setGravity() TextView 클래스
inputType setRawInputType() TextView 클래스
password setTransformationMethod() TextView 클래스
text setText() TextView 클래스
textColor setTextColor() TextView 클래스
textSize setTextSize() TextView 클래스

 


버튼과 Edit Text

버튼과 에디트 텍스트는 사용자에게 어떤 값을 입력받기 위한 가장 기본적인 위젯이다. 두 위젯은 View 클래스와 TextView 클래스를 상속받음으로 비슷하게 사용이 가능하다.

<Text View

  • android:layout_width="match_prent"
  • android:layout_height="wrap_content"
  • android:text="위젯"/>

<Button

  • android:layout_width="match_prent"
  • android:layout_height="wrap_content"
  • android:text="위젯"/>

위와 같이 Button으로 바꾸어도 동작한다.


버튼을 클릭했을 때 동작하는 Java 코드 3단계

  1. 버튼 변수 선언
    1. Button mybutton; 
  2. 변수에 버튼 위젯 대입 
    1. mybutton = (Button) findViewById(R.id.button1);
  3. 버튼을 클릭할 때 동작하는 클래스 정의
    1. mybutton.setOnClickListener( new View.OnClickListener() {
          public void onClick(View v) {
              // 동작 내용을 이 부분에 코딩
          }
      });

에디트 텍스트의 값을 가져오는 Java 코드 3단계

  1. 에디트텍스트 변수 선언
    1. EditText myEdit;
  2. 변수에 에디트텍스트 위젯 대입
    1. myEdit = (EditText) findViewById(R.id.edittext1);
  3. 에디트텍스트에 입력된 값 가져오기 → 주로 버튼 클릭 이벤트 리스너 안에 삽입
    1. String myStr = myEdit.getText().toString();
728x90