我正在创建一个具有自定义键盘的自定义控件。我遇到了几个问题。
我使用的是Visual Studio社区2019,并且使用Android_Accelerated_x86_Oreo(Android 8.1 API-27)作为我的Android模拟器。
总之,
我需要对Entry进行不同的样式设置,因此我从它创建了一个派生类,称为TextBoxEntry。
我在MainPage.cs中以编程方式创建了所有控件,因此MainPage.xaml是空的(差不多)。
Android特定代码(即渲染器)被塞进MainActivity.cs。
最后,定制键盘在Resources/xml/keyboard.xml下定义。
我无法解决这些问题(5个问题中的3个已解决):
1.为什么自定义呈现器的自定义键盘显示在控件下面?
1.当键盘出现时,我需要得到它的高度,这样我就可以相应地修改滚动视图的尺寸。()[EditText.FocusChange事件处理程序]不起作用,因为在那个时间点,键盘还没有显示,因此,它的高度为0。还是应该在其他事件中执行此操作?应该改用ViewTreeObserver. IOnGlobalLayoutListener。
1.键盘上的键是不可按的,有时候,键盘会隐藏起来,我猜当我按下键的时候,实际的输入就失去了焦点,不管怎么说,我实际上哪里出错了?
1.滚动视图的最后一列以及最后一行的TextBoxEntries被剪掉了。同样,我也不知道哪里出错了。这可以通过将margin设置为1来解决。
1.在ControlOnFocusChange()中,a_height给出1280,而b_height给出342。滚动视图占据了整个屏幕的一半以上,但它的高度远远不到整个屏幕的一半。这两个视图是否以不同的单位显示?根据Find our the default height of the controls in Xamarin Forms,似乎两个Height使用不同的单位...好的,这个问题可以通过一些转换来解决。
下面是所有相关文件的内容:
MainPage.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CustomKeyboard.MainPage"/>
MainPage.xaml.cs:
using Xamarin.Forms;
namespace CustomKeyboard
{
public class TextBoxEntry : Entry
{
public ScrollView ScrollView { get; set; } = null; // So that I could easily get its reference and work on it...
}
public partial class MainPage : ContentPage
{
AbsoluteLayout layout = null;
ScrollView scrollview = null;
Grid grid = null;
public MainPage()
{
InitializeComponent();
BuildControls();
BuildTextBoxes();
}
private void BuildControls()
{
layout = new AbsoluteLayout();
scrollview = new ScrollView();
grid = new Grid();
layout.Children.Add(scrollview, new Rectangle(0.5d, 0d, 0.95d, 0.85d), AbsoluteLayoutFlags.All);
scrollview.BackgroundColor = Color.Yellow;
scrollview.Padding = 5;
scrollview.Content = grid;
Content = layout;
}
private void BuildTextBoxes()
{
for (var i = 0; i < 50; ++i)
{
if (i % 5 == 0)
{
var l = new Label()
{
Text = (i / 5).ToString(),
HorizontalTextAlignment = TextAlignment.Center,
VerticalTextAlignment = TextAlignment.Center,
};
grid.Children.Add(l, 0, i / 5);
}
else
{
var r = i / 5;
var c = i % 5;
var e = new TextBoxEntry();
e.Margin = 1; // THIS SOLVES ISSUE 4
e.ScrollView = scrollview;
grid.Children.Add(e, c, r);
}
}
}
}
}
MainActivity.cs:
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.InputMethodServices;
using Android.OS;
using Android.Support.Design.Widget;
using Android.Views;
using Android.Widget;
using Java.Lang;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Xamarin.Essentials;
using CustomKeyboard;
using CustomKeyboard.Droid;
[assembly: ExportRenderer(typeof(TextBoxEntry), typeof(TextBoxEntryRenderer))]
namespace CustomKeyboard.Droid
{
[Activity(
Label = "CustomKeyboard",
Icon = "@mipmap/icon",
Theme = "@style/MainTheme",
MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation
)]
public class MainActivity : FormsAppCompatActivity
{
internal static MainActivity Instance { get; private set; }
protected override void OnCreate(Bundle savedInstanceState)
{
Instance = this;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
}
public class TextBoxEntryRenderer : ViewRenderer<TextBoxEntry, TextInputLayout>
{
public KeyboardView mKeyboardView;
ViewGroup activityRootView;
protected EditText editText => Control.EditText;
public TextBoxEntryRenderer(Context context) : base(context) { }
protected override TextInputLayout CreateNativeControl()
{
#region Add keyboard
var activity = MainActivity.Instance as Activity;
activity.Window.SetSoftInputMode(SoftInput.StateAlwaysHidden);
var rootView = activity.Window.DecorView.FindViewById(Android.Resource.Id.Content);
activityRootView = ((ViewGroup)rootView).GetChildAt(0) as ViewGroup;
mKeyboardView = new KeyboardView(MainActivity.Instance, null);
mKeyboardView.PreviewEnabled = false; //Removes magnified key popups on key press
mKeyboardView.Visibility = ViewStates.Gone;
mKeyboardView.Keyboard = new Android.InputMethodServices.Keyboard(Context, Resource.Xml.keyboard);
mKeyboardView.Key += (sender, e) =>
{
long eventTime = JavaSystem.CurrentTimeMillis();
DispatchKeyEvent(new KeyEvent(eventTime, eventTime, KeyEventActions.Down, e.PrimaryCode, 0, 0, 0, 0, KeyEventFlags.SoftKeyboard | KeyEventFlags.KeepTouchMode));
Task.Delay(1);
};
Android.Widget.RelativeLayout.LayoutParams layoutParams =
new Android.Widget.RelativeLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.WrapContent); // or wrap_content
layoutParams.AddRule(LayoutRules.AlignParentBottom);
activityRootView.AddView(mKeyboardView, layoutParams);
#endregion
var _editText = new EditText(Context);
_editText.SetTextColor(Android.Graphics.Color.Black);
var textInputLayout = new TextInputLayout(Context);
textInputLayout.AddView(_editText);
return textInputLayout;
}
protected override void OnElementChanged(ElementChangedEventArgs<TextBoxEntry> e)
{
base.OnElementChanged(e);
if (e.OldElement != null && Control != null) editText.FocusChange -= ControlOnFocusChange;
if (e.NewElement != null)
{
SetNativeControl(CreateNativeControl());
editText.ShowSoftInputOnFocus = false;
editText.FocusChange += ControlOnFocusChange;
editText.SetPadding(20, 0, 20, 0);
editText.SetTextSize(Android.Util.ComplexUnitType.Px, 32);
editText.TextChanged += (source, args) => { };
}
if (Control != null)
{
GradientDrawable gd = new GradientDrawable();
gd.SetColor(Android.Graphics.Color.Transparent);
Control.SetBackground(gd);
editText.Background = null; // Remove underline.
var shape = new ShapeDrawable(new Android.Graphics.Drawables.Shapes.RectShape());
shape.Paint.Color = Xamarin.Forms.Color.Gray.ToAndroid();
shape.Paint.SetStyle(Paint.Style.Stroke);
Control.Background = shape;
}
}
private void ControlOnFocusChange(object sender, FocusChangeEventArgs args)
{
if (args.HasFocus)
{
editText.Post(() =>
{
editText.RequestFocus();
if (mKeyboardView.Visibility == ViewStates.Gone) mKeyboardView.Visibility = ViewStates.Visible;
// Wanna resize scrollview, but couldn't get necessary info...!
var deviceDisplayInfo = DeviceDisplay.MainDisplayInfo;
var a_height = deviceDisplayInfo.Height;
var b_height = Element.ScrollView.Height;
var c_height = mKeyboardView.Height;
});
}
else
{
// Hide keyboard
mKeyboardView.Visibility = ViewStates.Gone;
}
}
}
}
最后,参考资料/xml/keyboard. xml:
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="25%p" android:horizontalGap="0px"
android:verticalGap="0px" android:keyHeight="27dip">
<Row>
<Key android:codes="8" android:keyLabel="1" android:keyHeight="54dip" android:keyEdgeFlags="left" />
<Key android:codes="9" android:keyLabel="2" android:keyHeight="54dip" />
<Key android:codes="10" android:keyLabel="3" android:keyHeight="54dip" />
<Key android:codes="81" android:keyLabel="+" android:keyHeight="81dip" android:keyEdgeFlags="right" />
</Row>
<Row></Row>
<Row>
<Key android:codes="11" android:keyLabel="4" android:keyHeight="54dip" android:keyEdgeFlags="left" />
<Key android:codes="12" android:keyLabel="5" android:keyHeight="54dip" />
<Key android:codes="13" android:keyLabel="6" android:keyHeight="54dip" />
</Row>
<Row>
<Key android:codes="66" android:keyLabel="RET" android:keyHeight="81dip" android:horizontalGap="75%p" android:keyEdgeFlags="right" />
</Row>
<Row>
<Key android:codes="14" android:keyLabel="7" android:keyHeight="54dip" android:keyEdgeFlags="left" />
<Key android:codes="15" android:keyLabel="8" android:keyHeight="54dip" />
<Key android:codes="16" android:keyLabel="9" android:keyHeight="54dip" />
</Row>
<Row></Row>
<Row>
<Key android:codes="17" android:keyLabel="*" android:keyHeight="54dip" android:keyEdgeFlags="left" />
<Key android:codes="7" android:keyLabel="0" android:keyHeight="54dip" />
<Key android:codes="18" android:keyLabel="#" android:keyHeight="54dip" />
<Key android:codes="67" android:keyLabel="DEL" android:keyHeight="54dip" android:keyEdgeFlags="right" />
</Row>
<Row></Row>
</Keyboard>
1条答案
按热度按时间uklbhaso1#
为什么自定义呈现器的自定义键盘显示在控件下面?
将
alignParentBottom
属性设置为true,使键盘从屏幕底部可见。键盘上的键是不可按的,有时候,键盘会隐藏起来,我猜当我按下键的时候,实际的输入就失去了焦点,不管怎么说,我实际上哪里出错了?
定义一个状态选择器,用于我们的键可以具有的两种状态:正常(未按下)和按下(正确或错误)。