我正在使用 TextWatcher 来验证我的输入,我希望我的红色提示图像是可点击的,这样我就可以给用户视觉反馈,如下图所示。到目前为止,我所拥有的是当我输入无效数据时出现的带有红色提示图像的编辑文本。我现在想知道如何使这个红色提示可点击并使弹出提示如图所示。
这是我到目前为止的代码,
public class MainActivity extends Activity implements TextWatcher{
EditText username,email = null;
boolean flag=false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username=(EditText) findViewById(R.id.usernametxt);
email=(EditText) findViewById(R.id.emailtxt);
username.addTextChangedListener(this);
username.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View view, MotionEvent motionEvent) {
// your code here...
if(flag)
{
email.setText("Error. . .");
}
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
return false;
}
});
Button loginbtn = (Button) findViewById(R.id.login);
loginbtn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//Check Validations
if(username.getText().toString().equalsIgnoreCase(""))
{
username.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.alert, 0);
flag=true;
}
}
});
Button clearbtn = (Button) findViewById(R.id.clear);
clearbtn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
username.setText("");
email.setText("");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
username.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
flag=false;
email.setText("");
}
}