Home > Net >  How to change the text field value from fragment to button click event
How to change the text field value from fragment to button click event

Time:12-09

Click on button event.

public void onClick(View view) {
        Button result = view.findViewById(view.getId());
        textNo[R.id.t1].setText("2");
        Toast.makeText(getActivity(),"클릭 : "   result.getText().toString(),Toast.LENGTH_SHORT).show();
    }

For example, is there a way to change the value of the first text view by clicking the first button? There are countless buttons, so I gave resource id to each button and text Now, for example, only 5 buttons and text views are given.

for (i = 0; i < 5; i  ) {
            String textId = "t"   (i   1);
            String buttonId = "b"   (i   1);
            textNo[i] = view.findViewById(getResources().getIdentifier(textId, "id", getActivity().getPackageName()));
            buttonNo[i] = view.findViewById(getResources().getIdentifier(buttonId, "id", getActivity().getPackageName()));

When I press the button on the same line, I want to increase the number of text views located on it, but I kept trying, but I couldn't figure out how to do it in the fragment. enter image description here

The outline of the xml code is as follows.

<TextView
            android:id="@ id/t1"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/zero" />
<Button
            android:id="@ id/b1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/button"
            android:layout_weight="1"
            android:text="@string/Click"
            style="?android:attr/buttonBarButtonStyle" />

It's my first time asking this question, so I'm very inexperienced, but if you tell me, I'll try harder. I'm sorry that I'm not good at English.

CodePudding user response:

You should use RecyclerView in this case. Your method isn'n correct

CodePudding user response:

b1.setOnClickListener { 
    t1.text = "tttt"
}

If u have only a few buttons, you can write them in xml. For more, use horizontal RecyclerView.

  • Related