如何通过用户键入值来增加微调器的数量
How to increase number of spinner by user key in value
如何通过用户填写类别行来添加微调器的数量?
这是我的片段代码。
请帮助我。
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.Inflate(Resource.Layout.Category1, container, false);
txtCat1Name = (EditText)rootView.FindViewById(Resource.Id.txtCat1Name);
txtCat1Length = (EditText)rootView.FindViewById(Resource.Id.txtCat1Length);
txtCat1Line = (EditText)rootView.FindViewById(Resource.Id.txtCat1Line);
btnSaveCat1 = (Button)rootView.FindViewById(Resource.Id.btnSaveCat1);
noOfLine = Int16.Parse(txtCat1Line.Text);
btnSaveCat1.Click += btnSaveCat1_click;
txtCat1Line.KeyPress += txtCat1Line_KeyPress;
return rootView;
}
private void txtCat1Line_KeyPress(object sender, View.KeyEventArgs e)
{
var spinners = new Spinner[0];
if (e.Event.Action == KeyEventActions.Up && txtCat1Line.Text.Length > 0)
{
for (int i = 0; i < noOfLine; i++)
{
var spinner = new Spinner(this.Activity);
spinners[i] = spinner;
}
}
}
如何通过用户在类别行中键入关键字来添加微调器。
您可以为 rootView 的 Xaml 定义根布局,id 为:
例如你的Layout.Category1
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id = "@+id/root_layout"
>
....
</LinearLayout>
然后在片段中:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.Inflate(Resource.Layout.Category1, container, false);
txtCat1Name = (EditText)rootView.FindViewById(Resource.Id.txtCat1Name);
txtCat1Length = (EditText)rootView.FindViewById(Resource.Id.txtCat1Length);
txtCat1Line = (EditText)rootView.FindViewById(Resource.Id.txtCat1Line);
btnSaveCat1 = (Button)rootView.FindViewById(Resource.Id.btnSaveCat1);
rootLayout = rootView.FindViewById<LinearLayout>(Resource.Id.root_layout);
btnSaveCat1.Click += btnSaveCat1_click;
txtCat1Line.KeyPress += txtCat1Line_KeyPress;
return rootView;
}
private void txtCat1Line_KeyPress(object sender, View.KeyEventArgs e)
{
e.Handled = false;
if (TextUtils.IsEmpty(txtCat1Line .Text))
{
return;
}
noOfLine = Int16.Parse(txtCat1Line .Text);
if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter)
{
for (int i = 0; i < noOfLine; i++)
{
var spinner = new Spinner(this.Activity);
layout.AddView(spinner);
e.Handled = true;
}
}
}
EditText xaml:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "@+id/txtCat1Line"
android:imeOptions="actionGo"
android:inputType="number"
/>
答案代码基本上提供了动态添加Spinner的方法,
在这里我建议不要监听 KeyPress 事件(也许你可以添加一个按钮来触发或监听焦点等)。
如何通过用户填写类别行来添加微调器的数量?
这是我的片段代码。 请帮助我。
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.Inflate(Resource.Layout.Category1, container, false);
txtCat1Name = (EditText)rootView.FindViewById(Resource.Id.txtCat1Name);
txtCat1Length = (EditText)rootView.FindViewById(Resource.Id.txtCat1Length);
txtCat1Line = (EditText)rootView.FindViewById(Resource.Id.txtCat1Line);
btnSaveCat1 = (Button)rootView.FindViewById(Resource.Id.btnSaveCat1);
noOfLine = Int16.Parse(txtCat1Line.Text);
btnSaveCat1.Click += btnSaveCat1_click;
txtCat1Line.KeyPress += txtCat1Line_KeyPress;
return rootView;
}
private void txtCat1Line_KeyPress(object sender, View.KeyEventArgs e)
{
var spinners = new Spinner[0];
if (e.Event.Action == KeyEventActions.Up && txtCat1Line.Text.Length > 0)
{
for (int i = 0; i < noOfLine; i++)
{
var spinner = new Spinner(this.Activity);
spinners[i] = spinner;
}
}
}
如何通过用户在类别行中键入关键字来添加微调器。
您可以为 rootView 的 Xaml 定义根布局,id 为:
例如你的Layout.Category1
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id = "@+id/root_layout"
>
....
</LinearLayout>
然后在片段中:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.Inflate(Resource.Layout.Category1, container, false);
txtCat1Name = (EditText)rootView.FindViewById(Resource.Id.txtCat1Name);
txtCat1Length = (EditText)rootView.FindViewById(Resource.Id.txtCat1Length);
txtCat1Line = (EditText)rootView.FindViewById(Resource.Id.txtCat1Line);
btnSaveCat1 = (Button)rootView.FindViewById(Resource.Id.btnSaveCat1);
rootLayout = rootView.FindViewById<LinearLayout>(Resource.Id.root_layout);
btnSaveCat1.Click += btnSaveCat1_click;
txtCat1Line.KeyPress += txtCat1Line_KeyPress;
return rootView;
}
private void txtCat1Line_KeyPress(object sender, View.KeyEventArgs e)
{
e.Handled = false;
if (TextUtils.IsEmpty(txtCat1Line .Text))
{
return;
}
noOfLine = Int16.Parse(txtCat1Line .Text);
if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter)
{
for (int i = 0; i < noOfLine; i++)
{
var spinner = new Spinner(this.Activity);
layout.AddView(spinner);
e.Handled = true;
}
}
}
EditText xaml:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "@+id/txtCat1Line"
android:imeOptions="actionGo"
android:inputType="number"
/>
答案代码基本上提供了动态添加Spinner的方法, 在这里我建议不要监听 KeyPress 事件(也许你可以添加一个按钮来触发或监听焦点等)。