런타임에서 동적 버튼에 대한 버튼 클릭 이벤트를 실행할 수 없습니다.

프리티 쉬

버튼 실행 시간 (앱에서)을 생성했습니다. 하지만 버튼 실행 시간의 이벤트를 처리 할 수 ​​없습니다. 내 시나리오에는 "이름"에 대한 두 개의 텍스트 상자 1과 "전화 번호"에 대한 다른 하나가 있습니다. 그리고 1 버튼 "추가". 추가 버튼을 클릭하면 "통화"와 "제거"버튼이 두 개있는 하나의 텍스트 상자를 동적으로 생성했습니다.

여기에 이미지 설명 입력 그러나 첫 번째 또는 두 번째 통화 버튼을 클릭하면 항상 마지막에 추가 된 번호로 전화를 겁니다.

코드는 다음과 같습니다.

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>

내 문제는 내가 특정 번호로 전화를 걸 수 있다는 것입니다. 하지만이 경우에는 마지막으로 추가 된 번호로만 전화를 걸 수 있습니다.

Sreeraj

호출 될 textOut때마다 덮어 씁니다 buttonAdd_Click. 따라서 새 레이아웃이 추가 textOut되면 항상 마지막으로 추가됩니다 EditText. 이것은 기본적으로 논리적 오류입니다. 논리에 따라 단일 인스턴스가 아닌 Listof 를 가져야 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();
        }
    }

위와 같이 코드를 변경하면 문제가 해결 될 수 있습니다. 나는 코드를 직접 테스트하지 않았습니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

클릭 이벤트를 수행 할 수없는 버튼에 클래스를 동적으로 적용

분류에서Dev

버튼에 대한 클릭 이벤트 또는 페이지 수명주기를 실행하지 않는 동적 버튼 (동적 아님)

분류에서Dev

jqGrid에서 버튼 클릭 이벤트를 추가 할 수 없습니다.

분류에서Dev

동적 버튼 클릭 이벤트에서 다른 매개 변수로 동일한 메소드를 호출 할 때 AngularJS 구문 오류

분류에서Dev

HTML 버튼에서 Google지도 마커 클릭 이벤트를 트리거 할 수 없습니다.

분류에서Dev

SAP B1 UI API에서 버튼 클릭 이벤트를 처리 할 수 없습니다.

분류에서Dev

버튼 클릭 이벤트에 List <myclass>를 전달할 수 있습니까?

분류에서Dev

대화 상자의 긍정적 인 버튼을 클릭 한 후 sharedPreferences에 데이터를 저장할 수 없습니다.

분류에서Dev

런타임 버튼은 항상 클릭 이벤트에서 마지막 버튼의 ID를 사용합니다.

분류에서Dev

HTML5의 동영상 태그에 대한 전체 화면 버튼 클릭 이벤트를 수동으로 실행

분류에서Dev

C #에서 동적으로 생성 된 버튼에 대한 클릭 이벤트

분류에서Dev

자바 스크립트에서 버튼의 클릭 이벤트에 액세스 할 수 없습니다.

분류에서Dev

클릭 할 때마다 여러 이벤트에 대한 버튼 생성

분류에서Dev

HTMLUnit에서 버튼 클릭 ()을 제출 한 후 새 페이지에 도달 할 수 없습니다.

분류에서Dev

한 번의 버튼 클릭으로 새 활동에 여러 데이터를 보낼 수 없습니다.

분류에서Dev

버튼 클릭 이벤트에 대한 동적 UL의 텍스트 상자 값

분류에서Dev

WPF에서 버튼의 아이콘은 전체 버튼을 클릭 할 수 없습니다.

분류에서Dev

탭 내부에 동적으로 추가 된 버튼에 대해 클릭 이벤트가 실행되지 않음

분류에서Dev

html 버튼을 클릭 한 후 아무것도 에코 할 수 없습니다.

분류에서Dev

다중 클릭 이벤트에 대한 Jquery 사용 버튼

분류에서Dev

MVC 4 면도기에서 버튼 클릭을 실행할 수 없습니까?

분류에서Dev

자바 스크립트에서 런타임에 버튼의 onClick 이벤트에 대한 기능을 얻을 수 없습니다.

분류에서Dev

Selenium IDE : 버튼 클릭 이벤트를 자동화 할 수 없음

분류에서Dev

프라임 페이스에서 라디오 버튼을 클릭 한 후 텍스트 상자를 표시 할 수 없습니다.

분류에서Dev

DataGridView에서 수동으로 버튼 클릭 이벤트 발생

분류에서Dev

동적 테이블보기 셀에서 버튼 크기를 제한 할 수 없습니다.

분류에서Dev

버튼을 클릭하는 동안 텍스트 상자에서 텍스트를 표시 할 수 없습니까?

분류에서Dev

C #에서 다른 버튼을 클릭 할 때 버튼 클릭 이벤트를 호출하는 방법

분류에서Dev

버튼 클릭 이벤트를 사용하여 서비스에서 새 활동을 열 수 있습니까?

Related 관련 기사

  1. 1

    클릭 이벤트를 수행 할 수없는 버튼에 클래스를 동적으로 적용

  2. 2

    버튼에 대한 클릭 이벤트 또는 페이지 수명주기를 실행하지 않는 동적 버튼 (동적 아님)

  3. 3

    jqGrid에서 버튼 클릭 이벤트를 추가 할 수 없습니다.

  4. 4

    동적 버튼 클릭 이벤트에서 다른 매개 변수로 동일한 메소드를 호출 할 때 AngularJS 구문 오류

  5. 5

    HTML 버튼에서 Google지도 마커 클릭 이벤트를 트리거 할 수 없습니다.

  6. 6

    SAP B1 UI API에서 버튼 클릭 이벤트를 처리 할 수 없습니다.

  7. 7

    버튼 클릭 이벤트에 List <myclass>를 전달할 수 있습니까?

  8. 8

    대화 상자의 긍정적 인 버튼을 클릭 한 후 sharedPreferences에 데이터를 저장할 수 없습니다.

  9. 9

    런타임 버튼은 항상 클릭 이벤트에서 마지막 버튼의 ID를 사용합니다.

  10. 10

    HTML5의 동영상 태그에 대한 전체 화면 버튼 클릭 이벤트를 수동으로 실행

  11. 11

    C #에서 동적으로 생성 된 버튼에 대한 클릭 이벤트

  12. 12

    자바 스크립트에서 버튼의 클릭 이벤트에 액세스 할 수 없습니다.

  13. 13

    클릭 할 때마다 여러 이벤트에 대한 버튼 생성

  14. 14

    HTMLUnit에서 버튼 클릭 ()을 제출 한 후 새 페이지에 도달 할 수 없습니다.

  15. 15

    한 번의 버튼 클릭으로 새 활동에 여러 데이터를 보낼 수 없습니다.

  16. 16

    버튼 클릭 이벤트에 대한 동적 UL의 텍스트 상자 값

  17. 17

    WPF에서 버튼의 아이콘은 전체 버튼을 클릭 할 수 없습니다.

  18. 18

    탭 내부에 동적으로 추가 된 버튼에 대해 클릭 이벤트가 실행되지 않음

  19. 19

    html 버튼을 클릭 한 후 아무것도 에코 할 수 없습니다.

  20. 20

    다중 클릭 이벤트에 대한 Jquery 사용 버튼

  21. 21

    MVC 4 면도기에서 버튼 클릭을 실행할 수 없습니까?

  22. 22

    자바 스크립트에서 런타임에 버튼의 onClick 이벤트에 대한 기능을 얻을 수 없습니다.

  23. 23

    Selenium IDE : 버튼 클릭 이벤트를 자동화 할 수 없음

  24. 24

    프라임 페이스에서 라디오 버튼을 클릭 한 후 텍스트 상자를 표시 할 수 없습니다.

  25. 25

    DataGridView에서 수동으로 버튼 클릭 이벤트 발생

  26. 26

    동적 테이블보기 셀에서 버튼 크기를 제한 할 수 없습니다.

  27. 27

    버튼을 클릭하는 동안 텍스트 상자에서 텍스트를 표시 할 수 없습니까?

  28. 28

    C #에서 다른 버튼을 클릭 할 때 버튼 클릭 이벤트를 호출하는 방법

  29. 29

    버튼 클릭 이벤트를 사용하여 서비스에서 새 활동을 열 수 있습니까?

뜨겁다태그

보관