1)傳遞一個接口,適配器
class MyActivity {
public interface OnCheckBoxClickListener{
public void OnCheckboxClicked();
}
public class MyOnCheckBoxClickListener :OnCheckBoxClickListener {
private WeakReference<Context> mContext;
public MyOnCheckBoxClickListener(Context context){
mContext = new WeakReference<Context>(context);
}
public void OnCheckboxClicked(){
Context context = null;
mContext.TryGetTarget(out context);
var editText = (context as Activity).FindViewById<EditText>(Resource.Id.edittextId);
editText.ClearFocus();
}
}
YourListAdapter adapter = new YourListAdapter(new MyOnCheckBoxClickListener(this));
}
2)然後:
private void hideKeyboard() {
View view = this.getCurrentFocus();
if (view != null) {
view.clearFocus();
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
您可以使用它的ListView:
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
hideKeyboard();
}
});
或複選框您的適配器:
class YourListAdapter {
OnCheckBoxClickListener mListener;
public override View GetView(int position, View convertView, ViewGroup parent) {
//code for view generation
var checkbox = convertView.FindViewById<Checkbox>(Resource.Id.YourCheckbox);
checkbox.Click += (sender, args) => {
mListener.OnCheckboxClicked();
}
}
}
此外,您可以使用委託實現同樣的事情。
我應該使用什麼代碼來關閉鍵盤呢?通常我使用(InputMethodManager)this.GetSystemService(Context.InputMethodService),但由於沒有Context,所以無法使用它。 –
您在您的活動中創建接口,然後將其傳遞給適配器構造函數 – Greensy
它說我不能在布爾值上使用+ =運算符 –