在我膨胀当前视图后 onClickListener() 不工作 (activity_main)

onClickListener() is not working after I inflate the current view(activity_main)

我想做的是 运行 一个 while 循环并在 textview 中打印文本,但是它只在第一次工作然后在第二次工作 onClickListener() 不工作。

这是我的 MainActivity 代码。

import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button inflate_Btn;
    EditText Get_number;
    ScrollView scrollView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);       
       setContentView(R.layout.activity_main);

       inflate_Btn=findViewById(R.id.inflate_Btn);
       Get_number=findViewById(R.id.Get_number);
       scrollView=findViewById(R.id.scrollView);
       inflate_Btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Integer number=Integer.parseInt(Get_number.getText().toString());
                if (number<=0 ||number==null){
                    Toast.makeText(getApplicationContext(),"Insert Number First!!",Toast.LENGTH_SHORT);
                    Get_number.requestFocus();
                }else {              
                    LayoutInflater inflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    View v=inflater.inflate(R.layout.activity_main,null);
                    ScrollView sv = (ScrollView) v.findViewById(R.id.scrollView);
                    LinearLayout ll = new LinearLayout(getApplicationContext());
                    ll.setOrientation(LinearLayout.VERTICAL);
                    Integer i=1;
                    while(i<=number)
                    {
                        TextView tv = new TextView(getApplicationContext());
                        tv.setText("my text"+i);
                        ll.addView(tv);
                        i++;
                    }
                    sv.addView(ll);
                    setContentView(v);
                }   
            }
        });
    }
}

我的 activity_main.xml 布局在这里。 我创建了一个简单的布局。请帮忙

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <EditText
        android:id="@+id/Get_number"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="Enter Number"
        android:inputType="number"/>

    <Button
        android:id="@+id/inflate_Btn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Inflate"></Button>

       <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
</LinearLayout>

试试这个。它就像一个魅力

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        inflate_Btn=findViewById(R.id.inflate_Btn);
        Get_number=findViewById(R.id.Get_number);
        scrollView=findViewById(R.id.scrollView);
        inflate_Btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Integer number=Integer.parseInt(Get_number.getText().toString());
                if (number<=0 ||number==null){
                    Toast.makeText(getApplicationContext(),"Insert Number First!!",Toast.LENGTH_SHORT);
                    Get_number.requestFocus();
                }else {
                    scrollView.removeAllViews();
                    LinearLayout ll = new LinearLayout(getApplicationContext());
                    ll.setOrientation(LinearLayout.VERTICAL);
                    Integer i=1;
                    while(i<=number)
                    {
                        TextView tv = new TextView(getApplicationContext());
                        tv.setText("my text"+i);
                        ll.addView(tv);
                        i++;
                    }
                    scrollView.addView(ll);
                }
            }
        });