使用EditText在AlertDialog或Fragment上显示软键盘

时间:2018-01-03 16:50:41

标签: android xamarin xamarin.android mvvmcross

我有两个实例,作为用户,我希望弹出软键盘,但不会。

当显示AlertDialog时,一个是我的Chance.MvvmCross.Plugings.UserInteraction的自定义分支。以下是代码:

public void Input(string message, Action<bool, string> answer, string hint = null, string title = null, string okButton = "OK", string cancelButton = "Cancel", string initialText = null, ValidationEnum validation = ValidationEnum.None)
{
    Application.SynchronizationContext.Post(ignored =>
    {

        if (CurrentActivity == null)
            return;

        var view = CurrentActivity.LayoutInflater.Inflate(Resource.Layout.input_dialog, null);
        EditText input = view.FindViewById<EditText>(Resource.Id.input_edit_text);
        input.InputType = Android.Text.InputTypes.TextFlagAutoCorrect
            | Android.Text.InputTypes.TextFlagCapWords
            | Android.Text.InputTypes.ClassText
            | Android.Text.InputTypes.TextFlagAutoComplete;
        input.Hint = hint;
        input.Text = initialText;
        input.RequestFocus();

        var dialog = new AlertDialog.Builder(CurrentActivity)
            .SetMessage(message)
            .SetTitle(title)
            .SetView(view)
            .SetPositiveButton(okButton, (sender, args) =>
                {                        
                    // nothing - setting a button listener below
                })
            .SetNegativeButton(cancelButton, delegate
            {
                answer?.Invoke(false, input.Text);
            }).SetCancelable(false)
            .Create();

        DisplayMetrics displayMetrics = new DisplayMetrics();
        dialog.Window.WindowManager.DefaultDisplay.GetMetrics(displayMetrics);
        int height = displayMetrics.HeightPixels;
        int dialogHeight = dialog.Window.DecorView.Height;
        WindowManagerLayoutParams wmlp = dialog.Window.Attributes;
        wmlp.Gravity = GravityFlags.Top;
        wmlp.Y = (height / 2 - dialogHeight) / 2;   // placing the middle of the dialog at about the 25% mark from the top

        dialog.Show();

        Button positiveButton = dialog.GetButton((int)DialogButtonType.Positive);
        positiveButton.SetOnClickListener(new ValidatingOnClickListener(dialog, input, answer, validation));

    }, null);
} 

在上面的例子中,我本来期望dialog.Show()强制软键盘向上,但它没有。想知道为什么?

第二个是包含AppCompatEditBox的Fragment。 FragmentView继承自BaseClass:

[MvxFragmentPresentation(typeof(MainViewModel), Resource.Id.main_content_frame, true)]
[Register("...AddItemFragment")]
public class AddItemFragment : BaseFragmentWithUpNavigation<AddItemViewModel>, ITextWatcher
{
    #region properties

    protected override int FragmentResourceId => Resource.Layout.fragment_add_item;
    ...

    #endregion

    #region Fragment lifecycle overrides

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        base.OnCreateView(inflater, container, savedInstanceState);

        var addItemEditText = FragmentView
            .FindViewById<AppCompatEditText>(Resource.Id.add_item_edittext);
        addItemEditText.AddTextChangedListener(this);       // to catch text change events
        addItemEditText.RequestFocus();

        ...

        return FragmentView;
    }
    ...
    #endregion
}

我知道很多代码但据我所知,RequestFocus应该强制显示软键盘吗?

1 个答案:

答案 0 :(得分:0)

  

在上面的例子中,我本来期望dialog.Show()强制软键盘向上,但它没有。想知道为什么?

您需要添加以下代码以强制显示软键盘:

1)在dialog.SetOnShowListener(this);之上添加dialog.Show();,如下所示:

dialog.SetOnShowListener(this);
dialog.Show();

2)实施IDialogInterfaceOnShowListener接口:

public void OnShow(IDialogInterface dialog)
{
    Timer timer = new Timer();
    timer.AutoReset = false;
    timer.Interval = 300;
    timer.Enabled = true;
    timer.Elapsed += Timer_Elapsed;
    timer.Start();
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
    InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
    imm.ShowSoftInput(input, ShowFlags.Implicit);
}

您需要处理时间延迟,因此,请使用System.Timers.Timer类来实现它。

在您的片段中,您只需添加Timer即可在AlterDialog中执行相同操作,以强制显示软键盘。