在android studio中每次点击按钮时增加数字

r1zk6ea1  于 2022-12-02  发布在  Android
关注(0)|答案(3)|浏览(410)

你好,我正在尝试设置一个计数器,每次点击增加一,但它没有响应。这是我的代码。我下面的教程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());
}
3b6akqbq

3b6akqbq1#

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函数

private void countMe(View view, View showCountTextView) {
    String countString = showCountTextView.getText().toString();
    Integer count = Integer.parseInt(countString);
    count++;

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

或者通过view得到TextView
1.从view取得showCountTextView

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());
}
iqxoj9l9

iqxoj9l92#

这里有两种可能的情况。
看起来,viewfragmentFirstLayout 在z轴上一个在另一个之上。因此,当您单击时,将单击 fragmentFirstLayout,而不是 view

  • View* 完全不作为片段视图。
uqcuzwp8

uqcuzwp83#

你应该继续学习教程。他们在步骤4中解决了问题。如果你仍然有问题,这里是FirstFragment.java文件,对我来说很有用。

package com.example.DTPM;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import com.example.DTPM.databinding.FragmentFirstBinding;

public class FirstFragment extends Fragment {

private FragmentFirstBinding binding;

TextView showCountTextView;

@Override
public View onCreateView(
        LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState
) {

    // Inflate the layout for this fragment
    View fragmentFirstLayout = inflater.inflate(R.layout.fragment_first, container, false);
    // Get the count text view
    showCountTextView = fragmentFirstLayout.findViewById(R.id.textview_first);

    return fragmentFirstLayout;

}

public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

//        binding.buttonFirst.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                NavHostFragment.findNavController(FirstFragment.this)
//                        .navigate(R.id.action_FirstFragment_to_SecondFragment);
//            }
//        });
        view.findViewById(R.id.toast_button).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Toast myToast = Toast.makeText(getActivity(), "Hello toast!", Toast.LENGTH_SHORT);
                myToast.show();
            }
        });
        view.findViewById(R.id.count_button).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                countMe(view);
            }
        });
    }

    private void countMe(View view){

    //View showCountTextView = view.findViewById(R.id.textview_first)
    // 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());
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    binding = null;
}

}

相关问题