How to make an EditText to be focused but not clickable?(Xamarin.Android)

一曲冷凌霜 提交于 2020-01-06 08:36:12

问题


I have 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" />

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.


回答1:


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


来源:https://stackoverflow.com/questions/56024589/how-to-make-an-edittext-to-be-focused-but-not-clickablexamarin-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!