如何使EditText成为焦点但不可点击?(Xamarin.Android)

如何使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;
    }
}