같은 결과를 도출하지만 약간씩 다른 방법으로 리스너를 구현했습니다.
콜백메서드 재정의
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View vw = new MyView(this);
setContentView(vw);
}
class MyView extends View{
public MyView(Context context){
super(context);
}
public boolean onTouchEvent(MotionEvent event){
super.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN){
Toast.makeText(this.getContext(), "touch Event Received",Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
리스너 인터페이스 구현
public class MainActivity extends AppCompatActivity {
TextView mResult;
final int READ_CONTACT_CODE = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View vw = new View (this);
vw.setOnTouchListener(TouchListener);
setContentView(vw);
}
class TouchListenerClass implements View.OnTouchListener {
public boolean onTouch(View v,MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN){
Toast.makeText(MainActivity.this,"touch Event Received",Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
return false;
TouchListenerClass TouchListener = new TouchListenerClass();
}
=========================================================
액티비티 리스너 구현
public class MainActivity extends AppCompatActivity implements View.OnTouchListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View vw = new View (this);
vw.setOnTouchListener(this);
setContentView(vw);
}
public boolean onTouch(View v,MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN){
Toast.makeText(MainActivity.this,"touch Event Received",Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
===================================
View에서 리스너 구현
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyView vw = new MyView (this);
vw.setOnTouchListener(vw);
setContentView(vw);
}
class MyView extends View implements View.OnTouchListener{
public MyView(Context context){
super(context);
}
public boolean onTouch(View v,MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN){
Toast.makeText(MainActivity.this,"touch Event Received",Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
}
=====================================
익명 내부 클래스 이용 리스너 구현
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View vw = new View (this);
vw.setOnTouchListener(TouchListener);
setContentView(vw);
}
View.OnTouchListener TouchListener = new View.OnTouchListener(){
public boolean onTouch(View v,MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN){
Toast.makeText(MainActivity.this,"touch Event Received",Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
};
}
===================================================================
<?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:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btnread"
android:layout_width="269dp"
android:layout_height="58dp"
android:layout_marginBottom="208dp"
android:text="주소록 읽기"/>
<Button
android:id="@+id/btnreset"
android:layout_width="267dp"
android:layout_height="55dp"
android:layout_marginBottom="136dp"
android:text="리셋" />
<TextView
android:id="@+id/result"
android:layout_width="244dp"
android:layout_height="61dp"
android:layout_marginBottom="188dp"
android:text="TextView" />
</LinearLayout>
---------------------------------------------------------------------
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout MyLinear = (LinearLayout) findViewById(R.id.linear);
MyLinear.setOnTouchListener(TouchListener);
}
View.OnTouchListener TouchListener = new View.OnTouchListener(){
public boolean onTouch(View v,MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN){
TextView text = (TextView)findViewById(R.id.result);
text.setText("touched");
return true;
}
return false;
}
};
}
'IT 통신 수학 과학 > 자바 안드로이드' 카테고리의 다른 글
menu기능//item.setIcon(R.drawable.ic_launcher)오류 (0) | 2020.09.02 |
---|---|
타이머 메시지 핸들러 (0) | 2020.08.29 |
버튼 리스너 구현 (0) | 2020.08.23 |
터치 입력시험 (0) | 2020.08.19 |
onRequestPermissionsResult error // READ_CONTACT_CODE error (0) | 2020.08.15 |
안드로이드 앱 권한 요청 (0) | 2020.08.02 |
google admob 시작하기 (0) | 2020.04.05 |
double to String (double을 문자열로 변경)/String to Double(문자열을 Double로 변경) (0) | 2020.03.21 |