在按钮的onClick事件中,如果编辑文本为空,我想要查询。如果一个编辑文本为null,我想显示一条消息并停止执行其余的代码。 `
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String client = txtClient.getText().toString();
if( txtClient.getText().toString().length() == 0 )
txtClient.setError( "First name is required!" ) ;
String adresa = txtAdresa.getText().toString();
String localitate = txtLocalitate.getText().toString();
String telefon = txtTelefon.getText().toString();
`
答案 0 :(得分:4)
使用return
关键字在if
区块的早期退出该功能:
if( txtClient.getText().toString().length() == 0 ) {
txtClient.setError( "First name is required!" );
return;
}
答案 1 :(得分:-2)
if( txtClient.getText().toString().length() == null ) {
txtClient.setError( "First name is required!" );
return;
}