在EditText中没有输入任何内容

时间:2016-05-02 06:51:58

标签: c# android xamarin android-edittext

如果焦点从第一个EditText改变,即mTxtFirstName,焦点转到mTxtEmail,则验证mTxtFirstName是否为空。所以按照以下步骤操作: -

  
      
  1. 新的焦点mTxtEmail将失去焦点,即mTxtEmail.ClearFocus()

  2.   
  3. mTxtFirstName将再次获得焦点,即mTxtFirstName.RequestFocus()

  4.   
  5. mTxtFirstName显示错误,即mTxtFirstName.SetError("非空字段",null)

  6.   
  7. 光标设置为第0位,即TxtFirstName.SetSelection(0)

  8.   
  9. 获取键盘输入,即ShowKeyboard(mTxtFirstName)

  10.   

所有事情都发生了但是当我开始输入mTxtFirstName时,键入发生在之前关注的EditText字段中。我也尝试使用模拟器键盘选项但没有成功。查看截图以获取更多信息。如何在mTxtFirstName中进行输入启用,直到它非空。请帮助。

输出屏幕:

enter image description here

这是.axml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#DEDEDC"
    android:minWidth="300dp"
    android:minHeight="400dp"
    android:id="@+id/MyDialog_Layout">
    <EditText
        android:theme="@android:style/Theme.Holo.Light"
        android:background="@drawable/edit_text_style"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="40dp"
        android:hint="First Name"
        android:paddingLeft="10dp"
        android:textColor="#000"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/txtFirstName" />
    <EditText
        android:theme="@android:style/Theme.Holo.Light"
        android:background="@drawable/edit_text_style"
        android_layout_below="@+id/txtFirstName"
        android:inputType="textEmailAddress"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="110dp"
        android:hint="Email"
        android:paddingLeft="10dp"
        android:textColor="#000"
        android:layout_width="match_parent"
        android:id="@+id/txtEmailId"
        android:layout_height="40dp" />
    <EditText
        android:theme="@android:style/Theme.Holo.Light"
        android:background="@drawable/edit_text_style"
        android:layout_below="@+id/txtEmailId"
        android:inputType="textPassword"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:hint="Password"
        android:paddingLeft="10dp"
        android:textColor="#000"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/txtPassword" />
    <EditText
        android:theme="@android:style/Theme.Holo.Light"
        android:background="@drawable/edit_text_style"
        android:layout_below="@+id/txtPassword"
        android:inputType="textPassword"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:hint="Confirm Password"
        android:paddingLeft="10dp"
        android:textColor="#000"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/txtConfirmPassword" />
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtPassword"
        android:layout_marginTop="50dp">
        <Button
            android:text="Sign Up"
            android:layout_below="@+id/txtConfirmPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btnDialogEmail"
            android:layout_weight="15"
            android:background="@drawable/ButtonSignUpStyle"
            android:layout_marginLeft="30dp"
            android:textSize="20dp"
            android:textColor="#fff"
            android:textStyle="bold"
            android:layout_marginBottom="50dp"
            android:layout_marginTop="30dp"
            android:paddingLeft="5dp"
            android:paddingBottom="5dp"
            android:paddingRight="5dp"
            android:height="70dp"
            android:paddingTop="2dp"
            android:layout_centerHorizontal="true" />
    </RelativeLayout>
</RelativeLayout>

以下是来自.cs文件的代码

 public override void OnResume()
        {
            base.OnResume();


    mTxtFirstName.FocusChange += (object sender, View.FocusChangeEventArgs e) =>

                {
                    if (!e.HasFocus)
                    {
                        if (string.IsNullOrEmpty(mTxtFirstName.Text.Trim()))

                        {

                                {
                                    mTxtEmail.ClearFocus();
                                    mTxtFirstName.RequestFocus();
                                    mTxtFirstName.Focusable = true;
                                    mTxtFirstName.SetError("Non Empty Field", null);
                                    mTxtFirstName.SetSelection(0);
                                    ShowKeyboard(mTxtFirstName);
                                }
                        }
                    }

                };


            }


            public static void ShowKeyboard(View pView)
            {
                    InputMethodManager inputMethodManager = Application.Context.GetSystemService(Context.InputMethodService) as InputMethodManager;
                    inputMethodManager.ShowSoftInput(pView, ShowFlags.Forced);
                    inputMethodManager.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);
            }

}

2 个答案:

答案 0 :(得分:0)

将您的xml代码更改为

 android:theme="@android:style/Theme.Holo.Light"
        android:background="@drawable/edit_text_style"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="40dp"
        android:hint="First Name"
        android:paddingLeft="10dp"
        android:textColor="#000"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/txtFirstName"
            android:focusable="true"
            android:focusableInTouchMode="true" />

并在按钮单击侦听器上更改java代码

 if ( mTxtFirstName.length() == 0 ||  mTxtFirstName.isEmpty() ) {
                     mTxtFirstName.setError("First Name Required");
}

还会在明确指定活动的地方添加

android:windowSoftInputMode="stateHidden"

删除

  mTxtEmail.ClearFocus();
                                    mTxtFirstName.RequestFocus();
                                    mTxtFirstName.Focusable = true;
                                    mTxtFirstName.SetError("Non Empty Field", null);
                                    mTxtFirstName.SetSelection(0);
                                    ShowKeyboard(mTxtFirstName);

试试这个

答案 1 :(得分:0)

您无法将焦点设置回FocusChange侦听器(事件)中的同一发件人,而不会在UI中引起问题(例如双焦点指示符和无法触摸(设置焦点)并使用窗口小部件执行编辑

但是当收到焦点到另一个小部件时,您可以ClearFocus当前小部件和RequestFocus到您的小部件输入错误:

protected override void OnResume()
{
    base.OnResume();

    EditText txtFirstName = FindViewById<EditText>(Resource.Id.txtFirstName);
    EditText txtEmailId = FindViewById<EditText>(Resource.Id.txtEmailId);
    EditText textPassword = FindViewById<EditText>(Resource.Id.txtPassword);
    EditText txtConfirmPassword = FindViewById<EditText>(Resource.Id.txtConfirmPassword);

    EventHandler<View.FocusChangeEventArgs> reFocusOnError = (object sender, View.FocusChangeEventArgs e) =>
    {
        if (string.IsNullOrEmpty(txtFirstName.Text.Trim()))
        {
            var editTextSender = sender as EditText;
            if (editTextSender.Id != Resource.Id.txtFirstName)
            {
                editTextSender.ClearFocus();
                txtFirstName.SetError("Non Empty Field", null);
                txtFirstName.Focusable = true;
                txtFirstName.ShowSoftInputOnFocus = true;
                txtFirstName.RequestFocus();
                InputMethodManager inputManager = (InputMethodManager)GetSystemService(Context.InputMethodService);
                inputManager.ShowSoftInput(txtFirstName, ShowFlags.Implicit);
            }

        }
    };

    txtFirstName.FocusChange += reFocusOnError;
    txtEmailId.FocusChange += reFocusOnError;
    textPassword.FocusChange += reFocusOnError;
    txtConfirmPassword.FocusChange += reFocusOnError;
}

enter image description here