358
No
class MainActivity
package com.example.choi.mystudy13;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View v){
Intent intent=null;
switch (v.getId()){
case R.id.button1:
intent=new Intent(MainActivity.this, SampleOneActivity.class);
break;
case R.id.button2:
intent=new Intent(MainActivity.this, SampleTwoActivity.class);
break;
case R.id.button3:
intent=new Intent(MainActivity.this, SampleThreeActivity.class);
break;
}
startActivity(intent);
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="com.example.choi.mystudy13.MainActivity">
<Button
android:text="터치 이벤트"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onClick"
android:background="@android:color/holo_blue_dark"
android:textColor="@android:color/background_light" />
<Button
android:text="리스너를 이용한 이벤트 처리"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:id="@+id/button2"
android:background="@android:color/holo_green_dark"
android:textColor="@android:color/background_light"
android:onClick="onClick"
/>
<Button
android:text="다양한 뷰를 이용한 이벤트 처리"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:id="@+id/button3"
android:background="@android:color/holo_orange_dark"
android:textColor="@android:color/background_light"
android:onClick="onClick"
/>
</RelativeLayout>
class SampleOneActivity
package com.example.choi.mystudy13;
import android.content.Context;
import android.graphics.Canvas;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class SampleOneActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CustomView customView =new CustomView(this);
setContentView(customView);
}
public class CustomView extends View{
private Context mContext;
public CustomView(Context context) {
super(context);
this.mContext=context;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
Toast.makeText(this.mContext, "JUNHO - ACTION_DOWN",
Toast.LENGTH_SHORT
).show();
return true;
}
return false;
}
}
}
class SampleTwoActivity
package com.example.choi.mystudy13;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class SampleTwoActivity extends AppCompatActivity {
RelativeLayout rl;
Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_two);
rl=(RelativeLayout)findViewById(R.id.sample_two);
//짧은 클릭 익명리스너
rl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tostShow("OnClick - 짧은 클릭");
}
});
//길게 클릭
rl.setOnLongClickListener(longClickListener);
}
View.OnLongClickListener longClickListener =new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
tostShow("onLongClick - 긴 클릭");
return false;
}
};
private void tostShow(String message){
if(toast==null){
toast=Toast.makeText(SampleTwoActivity.this, message, Toast.LENGTH_SHORT) ;
}else{
toast.setText(message);
}
toast.show();
}
}
R.layout.sample_two
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/sample_two"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="com.example.choi.mystudy13.SampleTwoActivity"
>
</RelativeLayout>
class SampleThreeActivity
package com.example.choi.mystudy13;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class SampleThreeActivity extends AppCompatActivity {
Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_three);
TextView tv=(TextView)findViewById(R.id.tv);
Button bt =(Button)findViewById(R.id.bt);
ImageView iv =(ImageView)findViewById(R.id.iv);
EditText et=(EditText)findViewById(R.id.et);
SeekBar sb =(SeekBar)findViewById(R.id.sb);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tostShow("텍스트 클릭 : setOnClickListener");
}
});
bt.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
tostShow("버튼 길게 클릭 : setOnLongClickListener");
return true;
}
});
iv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
tostShow("이미지 터치 : setOnTouchListener");
return true;
}
});
et.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
tostShow("텍스트 체인지 : addTextChangedListener :beforeTextChanged " +s.toString());
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
tostShow("텍스트 체인지 : addTextChangedListener :onTextChanged " +s.toString());
}
@Override
public void afterTextChanged(Editable s) {
tostShow("텍스트 체인지 : addTextChangedListener :afterTextChanged " +s.toString());
}
});
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
tostShow("시크바 : setOnSeekBarChangeListener :onStartTrackingTouch " +seekBar.getProgress());
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
public void tostShow(String message){
if(toast==null){
toast=Toast.makeText(SampleThreeActivity.this, message, Toast.LENGTH_SHORT);
}else{
toast.setText(message);
}
toast.show();
}
}
R.layout.sample_three
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/sample_three"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="com.example.choi.mystudy13.SampleThreeActivity">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="@+id/tv"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="30sp"
android:textStyle="normal|bold" />
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv"
android:id="@+id/bt"
android:textSize="14sp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/a1"
android:id="@+id/iv"
android:layout_below="@+id/bt"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/et"
android:layout_below="@+id/iv"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="input text"
android:background="@android:color/holo_green_light"
android:textSize="30sp" />
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/et"
android:layout_marginTop="13dp"
android:id="@+id/sb"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
댓글 ( 4)
댓글 남기기