How to make an EditText to be focused but not clic

2019-08-30 01:51发布

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条回答
▲ chillily
2楼-- · 2019-08-30 02:30

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;
    }
}
查看更多
登录 后发表回答