这是我的主要活动:
package com.santosh.sampleapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
public class Main extends Activity
{
EditText password_field;
TextView tv_err;
Button login;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
password_field = (EditText) findViewById(R.id.password_field);
tv_err = (TextView)findViewById(R.id.tv_err);
login = (Button)findViewById(R.id.login);
final String password = password_field.getText().toString();
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (password.equals("password")) {
tv_err.setText("good pass");
} else {
tv_err.setText("wrong pass");
}
} catch (Exception e) {
tv_err.setText(e.toString());
}
}
});
}
}
即使我password
在文本视图中输入,它也会向我显示一条wrong pass
消息。
如果有帮助,这是我的src/layout/main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText android:id="@+id/password_field"
android:hint="@string/password_hint"
android:inputType="textPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button android:id="@+id/login"
android:text="@string/get_message"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<TextView android:id="@+id/tw_err"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</LinearLayout>