Reputation: 67
How to add number of spinner by user fill in in category line ?
here is my code in fragment. please help me.
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;
}
}
}
how to add spinner by user key in in category line.
Upvotes: 0
Views: 188
Reputation: 14991
You could define a root layout for the Xaml of the rootView with an id :
e.g your 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>
then in the fragment :
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;
}
}
}
the EditText xaml:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "@+id/txtCat1Line"
android:imeOptions="actionGo"
android:inputType="number"
/>
The answer code basically provides a dynamic add Spinner method, and here I recommend not listening on the KeyPress event(Maybe you could add a button to trigger or listen for focus and so on).
Upvotes: 0