본문 바로가기

Android

이벤트 예제 - 클릭

Activity_main.xml

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

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

 

    <TextView

        android:id="@+id/count"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="0"

        android:textColor="#ffff00"

        android:textSize="40sp" />

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content" >

 

        <Button

            android:id="@+id/decrese"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="감소" />

       

          <Button

            android:id="@+id/increse"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="증가" />

         

    </LinearLayout>

 

</LinearLayout>

 

 

MainActivity.java

 

package com.example.testex21;

 

import android.os.Bundle;

 

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.widget.TextView;

 

public class MainActivity extends Activity {

     int count = 0;

     TextView textView;

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

          

           textView = (TextView)findViewById(R.id.count);

          

           findViewById(R.id.increse).setOnClickListener(clickListener);

           findViewById(R.id.decrese).setOnClickListener(clickListener);

          findViewById(R.id.decrese).setOnLongClickListener(longClickListener);

          findViewById(R.id.increse).setOnLongClickListener(longClickListener);

     }

 

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

     }

 

     private View.OnClickListener clickListener = new View.OnClickListener() {

          

           @Override

           public void onClick(View v) {

                // TODO Auto-generated method stub

               

                switch (v.getId()) {

                case R.id.decrese:

                     count--;

                     textView.setText(""+count);

                     break;

                case R.id.increse:

                     count++;

                     textView.setText(""+count);

                     break;

                default:

                     break;

                }

           }

     };

    

     private View.OnLongClickListener longClickListener = new View.OnLongClickListener() {

          

           @Override

           public boolean onLongClick(View v) {

                // TODO Auto-generated method stub

               

                switch (v.getId()) {

                case R.id.decrese:

                     count = 0;

                     textView.setText(""+count);

                     break;

                case R.id.increse:

                     count = 100;

                     textView.setText(""+count);

                     break;

                default:

                     break;

                }

               

                return false;

           }

     };

}




 

'Android' 카테고리의 다른 글

버튼 - 콤보박스  (0) 2013.03.28
텍스트뷰(textview) - 기본속성  (0) 2013.03.28
입력 - 터치 실습  (0) 2013.03.28
입력 - 터치(touch)  (0) 2013.03.28
이벤트 처리 - 리스너 인터페이스 구현  (0) 2013.03.27