Bước 1: Mở file res-> layout ->layout_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| android:layout_width = "match_parent" android:layout_height = "match_parent" android:paddingLeft = "10dp" android:paddingRight = "10dp" android:paddingTop = "20dp" tools:context = ".MainActivity" > < EditText android:id = "@+id/et1" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:hint = "@string/hello_world" /> < TextView android:id = "@+id/tv1" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:layout_below = "@+id/et1" /> </ RelativeLayout > |
Bước 2: Mở file src-> MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
| package com.example.activitydemo2; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MotionEvent; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private EditText et1; private TextView tv1; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); et1 = (EditText) findViewById(R.id.et1); tv1 = (TextView) findViewById(R.id.tv1); // lang nghe su kien EditText thay doi et1.addTextChangedListener( new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub // phuong thuc goi khi Text trong EditText duoc thay doi // gia tri s o trong phuong thuc nay da la gia tri cua chuoi trong EditText // da duoc thay doi Log.d( "debug" , "onTextChanged s: " + s + " start: " + start + " before: " + before + " count: " + count); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub // phuong thuc goi truoc khi Text trong EditText duoc thay doi // s la gia tri chuoi truoc khi thay doi start la vi tri chuoi bi thay doi // neu after = 1 co nghi la chuoi da duoc them 1 ky tu vao phia sau vi tri start // neu la xoa ky tu thi after = 0 va gia tri before trong phuong thuc onTextChanged se =1 Log.d( "debug" , "beforeTextChanged s: " + s + " start: " + start + " after: " + after + " count: " + count); } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub // phuong thuc goi sau khi Text trong EditText duoc thay doi Log.d( "debug" , "s: " + s); // khi chuoi da duoc thay doi minh set gia tri EditText cho TextView tv1.setText(s); } }); } // co 2 cach de bat su kien bam vao phim Back // 1 la override onBackPressed() // 2 la kiem tra keyCode == KeyEvent.KEYCODE_BACK nhu trong phuong thuc // onKeyDown @Override public void onBackPressed() { // TODO Auto-generated method stub // thuc hien lenh khi bam vao phim Back Log.d( "debug" , "1. onBackPressed" ); super .onBackPressed(); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub // phuong thuc onTouchEvent duoc goi khi nguoi dung // cham vao man hinh trong vi du nay khi nguoi dung // cham vao man hinh (ACTION_DOWN) nhac tay len (ACTION_UP) // di chuyen tren man hinh(ACTION_MOVE) minh se ghi log lai if (event.getAction() == MotionEvent.ACTION_DOWN) { Log.d( "debug" , "ACTION_DOWN" ); } if (event.getAction() == MotionEvent.ACTION_UP) { Log.d( "debug" , "ACTION_UP" ); } if (event.getAction() == MotionEvent.ACTION_MOVE) { float x = event.getX(); float y = event.getY(); Log.d( "debug" , "ACTION_MOVE " + "x: " + x + " y: " + y); } return super .onTouchEvent(event); } @Override public boolean onKeyDown( int keyCode, KeyEvent event) { // TODO Auto-generated method stub // phuong thuc onKeyDown duoc goi khi co su kien 1 phim duoc bam xuong // trong vi du nay minh bat su kien bat vao cac Phim Cung tren DT // android // va log ra man hinh if (keyCode == KeyEvent.KEYCODE_BACK) { Log.d( "debug" , "2. Press BACK" ); } if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { Log.d( "debug" , "Press VOLUME DOWN" ); } if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) { Log.d( "debug" , "Press VOLUME UP" ); } return super .onKeyDown(keyCode, event); } @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 ; } } |
0 nhận xét:
Đăng nhận xét