如何通过用户填写类别行来增加微调器数量?
这是我的代码片段。 请帮助我。
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;
}
}
}
答案 0 :(得分:1)
您可以使用id为rootView的Xaml定义根布局:
例如您的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事件(也许您可以添加一个按钮来触发或监听焦点等)。