转自论坛:http://bbs.youkuaiyun.com/topics/390354882
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
|
package
com.test;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.util.Log;
import
android.view.Gravity;
import
android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.TextView;
import
android.widget.Toast;
/**
*
* @author yejch
*
*/
public
class
Login
extends
Activity {
private
TextView tv1,tv2,tv3,tv4;
private
EditText et1,et2;
private
Button btn1,btn2;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super
.onCreate(savedInstanceState);
setContentView(R.layout.login);
tv1=(TextView)findViewById(R.id.tv1);
tv2=(TextView)findViewById(R.id.tv2);
tv3=(TextView)findViewById(R.id.tv3);
tv4=(TextView)findViewById(R.id.tv4);
et1=(EditText)findViewById(R.id.et1);
et2=(EditText)findViewById(R.id.et2);
btn1=(Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
String tv1Str = tv1.getText().toString();
String tv2Str = tv2.getText().toString();
Log.d(
"tv1: "
, tv1Str );
Log.d(
"tv1: "
, tv2Str );
btn1.setOnClickListener(
new
Button.OnClickListener(){
@Override
public
void
onClick(View v) {
String userName = et1.getText().toString();
String password = et2.getText().toString();
Log.d(
"用户名: "
, userName );
Log.d(
"密码: "
, password );
if
(
"yjc"
.equals(userName) &&
"123456"
.equals(password)) {
Log.d(
"登录成功!"
,
"登录成功O(∩_∩O)哈哈~"
);
ShowMessage(
"登录成功!"
);
}
else
{
ShowMessage(
"对不起,用户名或密码错误!"
);
}
}
});
}
public
void
ShowMessage(String str){
//Toast是一种提供给用户简洁信息的视图,Toast类帮助你创建和显示该信息。
Toast to = Toast.makeText(
this
, str, Toast.LENGTH_SHORT);
to.setGravity(Gravity.TOP,
0
,
220
);
//取得提示信息在屏幕上显示的位置
to.show();
}
}
|
注意:
把 String userName = et1.getText().toString();
String password = et2.getText().toString();放OnClick里面
最开始完整的代码是正确的,已经把以上两句代码放进去了。