티스토리 뷰
먼저 화면과 xml코드를 보여드리겠습니다
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#7500FF"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="50dp"
android:hint="적어주세요"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="50dp"
android:onClick="button_click"
android:text="전송!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
먼저 위와 같이 xml에 입력합니다
EditText에 hint는 에디트텍스트에 아무것도 안 적혀있을 때 나타낼 문구를 적어주시면 됩니다
inputType은 용도에 맞게 설정해주시면 됩니다
위에선 text로 지정했지만 number을 지정한다면 숫자만 적을 수 있으며 종류가 많이 있습니다
Button에서 onClick은 버튼 클릭시 java에 button_click함수를 호출합니다
package com.ehsehsl.osz;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
editText = findViewById(R.id.editText);
}
public void button_click(View view){
textView.setText(editText.getText().toString());
}
}
앱이 실행되면 onCreate가 실행되며 xml에 만들어둔 텍스트뷰와 에디트텍스트를 변수에 저장합니다
버튼 클릭시 button_click가 호출되어 텍스트뷰에 에디트텍스트에 적혀있는 텍스트를 받아와서 저장합니다
여기서 주의하실 점은 onClick으로 java에 있는 함수를 호출할때는 (View view)를 입력해주셔야 합니다(view는 원하는 대로 바꿔서 적어도 됨)
전송! 을 눌렀을 때 에디트텍스트에 있는 문구가 지워지도록 하고 싶다면
editText.setText("");
위 코드를 textView.setText(editText.getText().toString()); 밑에 추가해주시면 됩니다
'안드로이드 네이티브' 카테고리의 다른 글
안드로이드 터치 이벤트 자바로 구현하기 (0) | 2020.09.05 |
---|---|
안드로이드 앱에 사진 넣는 방법 (0) | 2020.09.05 |
안드로이드 텍스트뷰 최대 글자 수, 최대 줄 수 제한하기 (0) | 2020.09.04 |
안드로이드 상단바 없애기 (0) | 2020.09.04 |
안드로이드 스크롤뷰로 상하, 좌우 스크롤 하기 (0) | 2020.09.04 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- inputbox
- 안드로이드네이티브
- 안드로이드앱개발
- 자동답장
- 안드로이드
- 자바스크립트
- 안드로이드클라이언트
- MouseMove
- 카카오톡
- 코틀린
- 채팅
- Flutter
- 구구단어플
- 노래
- 구구단앱
- 오토핫키
- 안드로이드스튜디오
- 앱개발
- 플러터
- loop
- 구구단공부
- 로또
- 매크로
- JS
- 챗봇
- sendinput
- 안드로이드앱
- 명언
- 구구단
- 카카오봇
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함