gpt4 book ai didi

c# - 单击 ListView 中的复选框时隐藏键盘

转载 作者:行者123 更新时间:2023-11-30 01:29:07 28 4
gpt4 key购买 nike

我目前有 ListView,其中每个项目都有一个 TextView 和一个 CheckBox。在 ListView 之上是一个用于搜索 ListView 的 EditText。当用户搜索某些内容并单击其中一个复选框或在给定结果列表中滚动时,我希望键盘消失。我怎样才能做到这一点?

我已经使用 InputMethodManager 在我的其他 Activity 中处理了键盘,但我无法在适配器内部调用它,因为 EditText 超出了它的范围,我无法在使用适配器的 Activity 中调用它,因为到目前为止因为它关注的是单击 CheckBox 时没有任何变化。

谁能指出我正确的方向?

提前致谢

最佳答案

1) 将接口(interface)传递给您的适配器

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) 然后在你的适配器中:

    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();
}
}
}

您也可以使用委托(delegate)实现相同的目的。

关于c# - 单击 ListView 中的复选框时隐藏键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36007181/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com