如何使EditText成为焦点但不可点击?(Xamarin.Android) [英] How to make an EditText to be focused but not clickable?(Xamarin.Android)

查看:155
本文介绍了如何使EditText成为焦点但不可点击?(Xamarin.Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个EditText:

<EditText
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:focusedByDefault="false"
   android:minWidth="25px"
   android:minHeight="25px"
   android:id="@+id/editText1" />
<EditText
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:focusedByDefault="true"
   android:clickable="false"
   android:id="@+id/editText2" />
<EditText
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:focusedByDefault="false"
   android:id="@+id/editText3" />

我希望在打开活动时第二个EditText处于聚焦状态(光标在其中闪烁),但是当我点击它时,我不希望键盘出现.

I want when the activity is opened the second EditText to be focused(with the cursor blinking in it) but when I tap on it I don't want the keyboard to show up.

推荐答案

这是我整理的一个适用于我的示例.使用此解决方案,您可以从布局文件中的所有EditText视图中删除"focusedByDefault"和"clickable"属性.

Here's an example I put together that works for me. And with this solution, you can remove the "focusedByDefault" and "clickable" attributes from all of your EditText views in your layout file.

public class MainActivity : AppCompatActivity, View.IOnTouchListener {

    private EditText editText2;

    protected override void OnCreate(Bundle savedInstanceState) {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.main);
        editText2 = FindViewById<EditText>(Resource.Id.editText2);
        editText2.RequestFocus();
        editText2.SetOnTouchListener(this);  // Requires addition of View.IOnTouchListener interface to class
    }

    public bool OnTouch(View v, MotionEvent e) {
        v.OnTouchEvent(e);
        var imm = (Android.Views.InputMethods.InputMethodManager)v.Context.GetSystemService(InputMethodService);
        imm?.HideSoftInputFromWindow(v.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);
        return true;
    }
}

这篇关于如何使EditText成为焦点但不可点击?(Xamarin.Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆