2017년 4월 12일 수요일

안드로이드 스튜디오 버튼 클릭 예제


버튼 클릭 에 대한 예제

Activity_Main

xml version="1.0" encoding="utf-8"?><android.support.constraint.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="com.example.buster.hellow.MainActivity">

    <TextView        android:id="@+id/text1"        android:layout_width="129dp"        android:layout_height="98dp"        android:text="Hello World!"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintHorizontal_bias="0.568"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintRight_toRightOf="parent"        app:layout_constraintTop_toTopOf="parent"        app:layout_constraintVertical_bias="0.414" />

    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button"        tools:layout_editor_absoluteX="155dp"        tools:layout_editor_absoluteY="337dp" />

</android.support.constraint.ConstraintLayout>




MainActivity.java

package com.example.buster.hellow;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn1 =(Button)findViewById(R.id.button);
        btn1.setOnClickListener(niced);



    }
    Button.OnClickListener niced = new View.OnClickListener() {
        public void onClick(View v) {
            TextView tex1 = (TextView)findViewById(R.id.text1);

            tex1.setText("버튼 클릭 후 글자가 표시됨");

        }


    };


}




댓글 없음: