[Android]안드로이드 스튜디오 버튼으로 text 보내기

    xml 파일

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    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"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity"
    >
    
    <TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="1"
    android:textSize="28dp"
    android:textColor="#000000"
    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="match_parent"
    android:layout_height="wrap_content"
    android:hint="Input Message"
    android:textSize="28dp"
    android:textColor="#000000"
    />
    
    <Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Push Button"
    android:textSize="28dp"
    android:textColor="#000000"
    />
    
    <TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="2"
    android:textSize="28dp"
    android:textColor="#000000"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    />
    
    <EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Input Message"
    android:textSize="28dp"
    android:textColor="#000000"
    />
    
    <Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Push Button"
    android:textSize="28dp"
    android:textColor="#000000"
    />
    
    </LinearLayout>
    
    

     

    자바파일

    package com.example.edittext;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button; //버튼뷰 사용
    import android.widget.EditText; //글자 입력창
    
    public class MainActivity extends AppCompatActivity {
    
    	private EditText editText;
    	private EditText editText1;
    	private Button button;
    	private Button button1;
    
    	@Override // 부모 메소드 재정의
    	protected void onCreate(Bundle savedInstanceState) { // 화면생성 이벤트
    		super.onCreate(savedInstanceState); // 부모 생성자 호출
    		setContentView(R.layout.activity_main); // 메인 화면 표시
    		editText = (EditText) findViewById(R.id.editText);
    		button = (Button) findViewById(R.id.button);
    		editText1 = (EditText) findViewById(R.id.editText1);
    		button1 = (Button) findViewById(R.id.button1);
    
    		button.setOnClickListener(new View.OnClickListener() { // 클릭리스너 생성
    			@Override // 부모 메소드 재정의
    			public void onClick(View v) { // 클릭 이벤트 처리
    				String str = editText.getText().toString();
    				editText.setText(""); // input message 부분
    				editText1.setText(str); // 1번의 텍스트를 2번의 에디트 텍스트로
    			}
    		});
    
    		button1.setOnClickListener(new View.OnClickListener() { // 클릭리스너 생성
    			@Override // 부모 메소드 재정의
    			public void onClick(View v) { // 클릭 이벤트 처리
    				String str = editText1.getText().toString();
    				editText1.setText("");
    				editText.setText(str);
    			}
    		});
    	}
    }

     

    출력 이미지

     

    1번의 input message에  글 입력후 푸시 버튼을 누르면 2번으로 넘어가고

    2번의 input message에  글 입력후 푸시 버튼을 누르면 1번으로 넘어간다.

    댓글

    Designed by JB FACTORY