Номер приращения при каждом нажатии кнопки в студии android - PullRequest
0 голосов
/ 24 марта 2020

Привет. Я пытаюсь установить счетчик для каждого клика, чтобы увеличить его на единицу, но он не отвечает. Это мой код Я следую этому уроку https://codelabs.developers.google.com/codelabs/build-your-first-android-app/#7

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_first, container, false);
   // View view1 = inflater.inflate(R.layout.fragment_first, container, false);
    View fragmentFirstLayout = inflater.inflate(R.layout.fragment_first, container, false);
    showCountTextView = fragmentFirstLayout.findViewById(R.id.text_first);
   // button = view.findViewById(R.id.toast_button);
    button = view.findViewById(R.id.count_button);


    view.findViewById(R.id.count_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            countMe(view);
        }
    });
    return  fragmentFirstLayout; //view;
}
private void countMe(View view) {
    // Get the value of the text view
    String countString = showCountTextView.getText().toString();
    // Convert value to a number and increment it
    Integer count = Integer.parseInt(countString);
    count++;
    // Display the new value in the text view.
    showCountTextView.setText(count.toString());
}

Ответы [ 2 ]

1 голос
/ 24 марта 2020

fragment: все элементы необходимо получить через view

View yourView = view.findViewById(R.id.text_first);

activity: можно напрямую получить доступ к элементу

View yourView = findViewById(R.id.text_first);

Ваш случай fragment, так что есть два способа сделать это

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_first, container, false);

    View showCountTextView = view.findViewById(R.id.text_first);
    Button button = view.findViewById(R.id.count_button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            countMe(view, showCountTextView); // 1
            countMe(view); // 2
        }
    });
    return view;
}
  1. передать showCountTextView на countMe веселье c
private void countMe(View view, View showCountTextView) {
    String countString = showCountTextView.getText().toString();
    Integer count = Integer.parseInt(countString);
    count++;

    showCountTextView.setText(count.toString());
}

или получить TextView view

получить showCountTextView от view
private void countMe(View view) {
    View showCountTextView = view.findViewById(R.id.text_first);

    String countString = showCountTextView.getText().toString();
    Integer count = Integer.parseInt(countString);
    count++;

    showCountTextView.setText(count.toString());
}
0 голосов
/ 24 марта 2020

Здесь возможны два случая.

Кажется, view и фрагментFirstLayout расположены друг над другом на оси z. Поэтому при нажатии фрагментFirstLayout будет нажат, а не представление .

ИЛИ

Представление не передается как фрагмент смотреть вообще.

...