我想验证“ username
”和“ password
”字段的编辑文本。我不希望用户将用户名和密码留为空白。我在这个站点中发现使用TextWatcher
n 添加addTextChangedListener
我们可以处理这个..但是出现强制关闭错误..我对android很陌生,这是我的第一个应用程序。请告诉我哪里出错了。我有以下代码:
public class MainActivity extends Activity implements android.text.TextWatcher {
public String userString;
public EditText edt_Username;
public EditText edt_pwd;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// for validating username textbox
edt_Username=(EditText) findViewById(R.id.username);
edt_Username.addTextChangedListener(this);
//for validating pwd textbox
edt_pwd=(EditText) findViewById(R.id.pwd);
edt_pwd.addTextChangedListener(this);
}
@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;
}
public void login(View view)
{
Intent intent=new Intent(this,Leave_form.class);
setContentView(R.layout.activity_leave_form);
startActivity(intent);
}
@Override
public void afterTextChanged(Editable s)
{
// TODO Auto-generated method stub
if( edt_Username.getText().toString().equals(null)||edt_pwd.getText().toString().equals(null))
{
Toast.makeText(MainActivity.this, "All Fields Required.",
Toast.LENGTH_SHORT).show();
}
}
提前致谢..