当用户在Edittext上添加任何单词时,我想显示一个隐藏按钮。 我已经尝试过这种方法:
((Button)findViewById(R.id.btnClear)).setVisibility(View.VISIBLE);
这是我在TextWatcher中的代码:
final EditText txtSearch = (EditText)findViewById(R.id.txtSearch);
txtSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
((Button)findViewById(R.id.btnClear)).setVisibility(View.VISIBLE);
CategoryCustomAdapter adapter2 =
new CategoryCustomAdapter(showCategoryListActivity.this,
DatabaseHelper.getCategories(
showCategoryListActivity.this,s.toString()));
((ListView)findViewById(R.id.lstCategorylist)).setAdapter(adapter2);
}
@Override
public void afterTextChanged(Editable s) {
}
});
除了这一行,每件事情都很好:
((Button)findViewById(R.id.btnClear)).setVisibility(View.VISIBLE);
我相信你可以帮助我。 ps:API 19 Nexus 4 android 4.4.2
答案 0 :(得分:0)
如前所述声明你变量-in Oncreate。
Button howtoButton = (Button) view.findViewById(R.id.howtobutton);
比这样使用
howtoButton.setVisibility(View.VISIBLE)
howtoButton.setVisibility(View.GONE)
答案 1 :(得分:0)
终于明白了。
日志说:
android.support.v7.widget.AppCompat ImageButton 无法强制转换为 android.widget。按钮强>
在onCreate()上更改此内容:
final Button btnClear = (Button)findViewById(R.id.btnClear);
到此:
final ImageButton btnClear = (ImageButton)findViewById(R.id.btnClear);
我不知道为什么但是我使用了一些背景作为我的Button以及一些如何转向 ImageButton 和btnClear.setVisibility(View.VISIBLE);
在public void onTextChanged(CharSequence s, int start, int before, int count)
中正常工作
public class showCategoryListActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_category_list);
final ListView lstCategory = (ListView) findViewById(R.id.lstCategorylist);
final ImageButton btnClear = (ImageButton)findViewById(R.id.btnClear);
final EditText txtSearch = (EditText)findViewById(R.id.txtSearch);
CategoryCustomAdapter adapter =
new CategoryCustomAdapter(this,DatabaseHelper.getCategories(
this,((TextView)findViewById(R.id.txtSearch)).getText().toString()));
lstCategory.setAdapter(adapter);
txtSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
btnClear.setVisibility(View.VISIBLE);
CategoryCustomAdapter adapter2 =
new CategoryCustomAdapter(showCategoryListActivity.this,
DatabaseHelper.getCategories(
showCategoryListActivity.this,s.toString()));
((ListView)findViewById(R.id.lstCategorylist)).setAdapter(adapter2);
}
@Override
public void afterTextChanged(Editable s) {
}
});
txtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
CategoryCustomAdapter adapter = new CategoryCustomAdapter(
showCategoryListActivity.this,
DatabaseHelper.getCategories(
showCategoryListActivity.this,
((TextView)findViewById(R.id.txtSearch)).getText().toString()));
((ListView)findViewById(R.id.lstCategorylist)).setAdapter(adapter);
return true;
}
return false;
}
});
}
public void btn_clear_clicked(View view) {
((TextView)findViewById(R.id.txtSearch)).setText("");
CategoryCustomAdapter adapter = new CategoryCustomAdapter(
showCategoryListActivity.this,
DatabaseHelper.getCategories(
showCategoryListActivity.this,
((TextView)findViewById(R.id.txtSearch)).getText().toString()
)
);
((ListView)findViewById(R.id.lstCategorylist)).setAdapter(adapter);
}
}
按钮的XML:
<ImageButton
android:id="@+id/btnClear"
android:layout_width="25dp"
android:layout_height="25dp"
android:text="Button"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@android:drawable/ic_menu_close_clear_cancel"
android:layout_marginTop="11dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:onClick="btn_clear_clicked (showCategoryListActivity)"
android:visibility="invisible"
android:elevation="0dp" />
&#13;
对不起我的粗心