运行时无法触发动态按钮的按钮单击事件

时间:2016-05-03 05:13:10

标签: android xamarin xamarin.android

我已生成按钮运行时间(在应用程序中)。但是我无法处理按钮运行时的事件。我的场景我有两个文本框1代表“名称”而另一个代表“电话号码”和1个按钮“添加”。当点击Add按钮时,我动态生成One Text框,带有两个按钮“Call”和“Remove”。

enter image description here 但是当点击第一个或第二个呼叫按钮时,它总是最后添加一个号码。

代码如下:

public class MainActivity : Activity
    {
        int count = 1;
        EditText textIn, txtPhoneNo;
        Button buttonAdd;
        LinearLayout container;
        EditText textOut;
        System.Collections.ArrayList arrList = new System.Collections.ArrayList();
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            textIn = (EditText)FindViewById(Resource.Id.textin);
            txtPhoneNo = (EditText)FindViewById(Resource.Id.txtPhoneNo);
            buttonAdd = (Button)FindViewById(Resource.Id.add);
            container = (LinearLayout)FindViewById(Resource.Id.container);
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            LayoutInflater layoutInflater = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
            View addView = layoutInflater.Inflate(Resource.Layout.row, null);
            textOut = (EditText)addView.FindViewById(Resource.Id.textout);
            arrList.Add(txtPhoneNo.Text);
            if (textIn.Text != "" && txtPhoneNo.Text != "")
            {
                textOut.SetText(textIn.Text + " : " + txtPhoneNo.Text, TextView.BufferType.Normal);
                container.AddView(addView);
                Button btnCall = (Button)addView.FindViewById(Resource.Id.btnCall);
                btnCall.Click += BtnCall_Click;
                Button buttonRemove = (Button)addView.FindViewById(Resource.Id.remove);
                buttonRemove.Click += ButtonRemove_Click;
            }
            else
            {
                Toast.MakeText(this, "Field can not be blank.", ToastLength.Short).Show();
            }
        }

        private void BtnCall_Click(object sender, EventArgs e)
        {
            var callDialog = new AlertDialog.Builder(this);
           string strNo = After(textOut.Text,":");
            callDialog.SetMessage("Call " + strNo + "?");
            callDialog.SetNeutralButton("Call", delegate
            {
                 var callIntent = new Intent(Intent.ActionCall);
                callIntent.SetData(Android.Net.Uri.Parse("tel:" + strNo));
                StartActivity(callIntent);
            });
            callDialog.SetNegativeButton("Cancel", delegate { });

            // Show the alert dialog to the user and wait for response.
            callDialog.Show();
        }
}
}

Main.axml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <EditText
        android:id="@+id/textin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:hint="name" />
    <EditText
        android:id="@+id/txtPhoneNo"
        android:layout_width="345.0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:hint="Phone No." />
    <Button
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Add" />
    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />
</LinearLayout>
row.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="wrap_content">
  <Button
      android:id="@+id/remove"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:text="Remove"/>
  <Button
      android:id="@+id/btnCall"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_toLeftOf="@id/remove"
      android:text="Call"/>
  <EditText
      android:id="@+id/textout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_toLeftOf="@id/remove"/>
</RelativeLayout>

我的问题是我怎么能打电话给特别没有。但在这种情况下,我只能拨打最后添加的号码。

1 个答案:

答案 0 :(得分:1)

每次调用textOut时,您都会覆盖buttonAdd_Click。因此,添加新布局时textOut始终是最后添加的EditText。这基本上是一个逻辑错误。根据您的逻辑,您应该List EditText而不是单个实例。

List<EditText> textOuts; // instead of EditText textOut;
int layoutCount=0;
protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.Main);

        textIn = (EditText)FindViewById(Resource.Id.textin);
        txtPhoneNo = (EditText)FindViewById(Resource.Id.txtPhoneNo);
        buttonAdd = (Button)FindViewById(Resource.Id.add);
        container = (LinearLayout)FindViewById(Resource.Id.container);
        textOuts= new List<EditText>();
    }
private void buttonAdd_Click(object sender, EventArgs e)
    {
        int i=layoutCount++;
        LayoutInflater layoutInflater = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
        View addView = layoutInflater.Inflate(Resource.Layout.row, null);
        textOuts.Add((EditText)addView.FindViewById(Resource.Id.textout));
        arrList.Add(txtPhoneNo.Text);
        if (textIn.Text != "" && txtPhoneNo.Text != "")
        {
            textOut.SetText(textIn.Text + " : " + txtPhoneNo.Text, TextView.BufferType.Normal);
            container.AddView(addView);
            Button btnCall = (Button)addView.FindViewById(Resource.Id.btnCall);
            btnCall.Click +=delegate(object sender, EventArgs e) {
            var callDialog = new AlertDialog.Builder(this);
       string strNo = After(textOuts[i].Text,":");
        callDialog.SetMessage("Call " + strNo + "?");
        callDialog.SetNeutralButton("Call", delegate
        {
             var callIntent = new Intent(Intent.ActionCall);
            callIntent.SetData(Android.Net.Uri.Parse("tel:" + strNo));
            StartActivity(callIntent);
        });
        callDialog.SetNegativeButton("Cancel", delegate { });

        // Show the alert dialog to the user and wait for response.
        callDialog.Show();
        };
            Button buttonRemove = (Button)addView.FindViewById(Resource.Id.remove);
            buttonRemove.Click += ButtonRemove_Click;
        }
        else
        {
            Toast.MakeText(this, "Field can not be blank.", ToastLength.Short).Show();
        }
    }

更改上面的代码可能会解决问题。我自己没有测试过代码。