android:id="@+id/btLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆"
android:layout_alignParentRight="true"
android:onClick="login"
/>
public class MainActivity extends Activity {
EditText etUserName = null;
EditText etPassword = null;
CheckBox cb = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUserName = (EditText) findViewById(R.id.etUserName);
etPassword = (EditText) findViewById(R.id.etPassword);
cb = (CheckBox) findViewById(R.id.cb);
}
public void login(View view){
String userName = etUserName.getText().toString().trim();
String password = etPassword.getText().toString().trim();
if(TextUtils.isEmpty(userName)||TextUtils.isEmpty(password)){
Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
} else {
//判断用户是否记住密码
if(cb.isChecked()){
//保存用户名密码
Log.i("tag", "需要记住密码");
}
//登陆发送消息到服务器,服务器验证是否正确
if("zhangsan".equals(userName)&&"123".equals(password)){
Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "登陆失败,用户名或密码错误", Toast.LENGTH_SHORT).show();
}
}
}
@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;
}
}
log中的不知道怎么复制上来----04-14 20:56:37.910: E/AndroidRuntime(1142): java.lang.IllegalStateException: Could not find a method login(View) in the activity class com.example.login.MainActivity for onClick handler on view class android.widget.Button with id 'btLogin'
android1.6以上才能使用,并且只能给Button控件使用,好像是这样的。你检查看看,我看没什么错误。
在xml中
在对应要监听的控件中做如下操作
android:onClick="selftDestruct"
................../>
在Activity中声明selftDestruct(View)方法,要用public修饰
即如:
public void selftDestruct(View view){
//这里就是监听要操作的代码
}