我想要禁用按钮,而不是在textview为空时单击,并在文本不为空时启用并获取单击事件。
以下是使用的代码
txtVesselName = (TextView) findViewById(R.id.text_VesselName);
btnPost = (Button)findViewById(R.id.button_addPost_Send);
txtVesselName.setVisibility(View.GONE);
etxtVesselCode = (EditText) findViewById(R.id.text_VesselCode);
etxtVesselCode.addTextChangedListener(vesselWatcher);
if(txtVesselName.getText().toString().isEmpty())
{
// btnPost.setClickable(true);
btnPost.setEnabled(false);
btnPost.setClickable(false);
btnPost.setBackgroundColor(Color.GRAY);
}
if(txtVesselName.getText().toString().trim() != null)
{
btnPost.setClickable(true);
btnPost.setEnabled(true);
}
btnPost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Click to this", "");
}
});
当我使用btnPost.setEnabled(false);
按钮时未禁用&可以在这种情况下点击。如何创建这样的行为。
下面是我的AddTextChangeListenerMethod()
方法
private final TextWatcher vesselWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
txtVesselName.setVisibility(View.VISIBLE);
}
public void afterTextChanged(Editable s) {
if (s.length() == 0)
{
txtVesselName.setVisibility(View.GONE);
}
else
{
SQLiteDatabase db = dbhelper.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from VesselList where Notation " + "= ? ", new String[]{etxtVesselCode.getText().toString().trim()});
if (cursor.moveToFirst())
{
do
{
strVesselsTypeName = cursor.getString(cursor.getColumnIndex("VesselsTypeName"));
Log.e("strVesselsTypeName", "=" + strVesselsTypeName);
SharedPreferences sp;
sp = getApplicationContext().getSharedPreferences(MyPREFERENCES, 0);
SharedPreferences.Editor e = sp.edit();
e.putString("xyz",etxtVesselCode.getText().toString().trim());
e.commit();
} while (cursor.moveToNext());
} db.close();
txtVesselName.setText(strVesselsTypeName);
btnPost.setEnabled(true);
Log.e("afterTextChanged"," = "+strVesselsTypeName);
}
}
};
答案 0 :(得分:0)
在afterTextChanged()中添加
btnPost.setClickable(true);
后,
btnPost.setEnabled(true);