Android - 如何让按钮听多次

时间:2021-08-17 18:24:19

I have an app with a home screen and a bunch of buttons on it, and therefore listeners for each. After the user clicks on one of the buttons, a new layout is brought up and that layout has a back button with a listener.

我有一个带有主屏幕和一堆按钮的应用程序,因此每个人都有听众。用户单击其中一个按钮后,将启动一个新布局,并且该布局具有一个带有侦听器的后退按钮。

The problem is that whenever the user presses the back button, the home screen layout is brought back up but none of the listeners work for the buttons anymore.

问题在于,每当用户按下后退按钮时,主屏幕布局就会恢复,但是没有一个听众再次为按钮工作。

Here is some sample code:

以下是一些示例代码:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); // return to home screen

    // sets up a listener for when the GCF main screen button is clicked.
    GCFButton.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {
            setContentView(R.layout.gcf); // change to the gcf layout
            Button back = (Button)findViewById(R.id.btnBack); // set up the back button in the gcf layout
            back.setOnClickListener(new View.OnClickListener() // put a listener on back button
            {
                public void onClick(View v) 
                {
                    setContentView(R.layout.main); // return to home screen
                }
            });

            Button GCFCalculate = (Button)findViewById(R.id.btnCalculate); // set up the gcf button in the gcf layout
            GCFCalculate.setOnClickListener (new View.OnClickListener() // put listener on gcf button in gcf layout
            {
                public void onClick(View v)
                {
                    // do stuff
                }
            });
        }
    });
}

3 个解决方案

#1


2  

You should not change a screen with setContentView(). Screens are changed in Android by starting a new Activity with startActivity(new Intent(...)) or with Fragments like recommended by Malimo (which is a bit more difficult to do but much nicer). You call two times setContentView() where one is destroying the other one.

您不应该使用setContentView()更改屏幕。通过启动一个带有startActivity的新Activity(新的Intent(...))或者像Malimo推荐的Fragments(这样做更难做但更好),在Android中更改了屏幕。你调用两次setContentView(),其中一个正在摧毁另一个。

#2


1  

in my opinion you should use fragments for your contentviews. so every fragment will be responsible for its contentview and can add listeners each time it is displayed... http://developer.android.com/guide/components/fragments.html

在我看来,你应该使用片段作为你的内容视图。因此每个片段都将负责其内容视图,并且每次显示时都可以添加监听器... http://developer.android.com/guide/components/fragments.html

#3


0  

I'm sure that there is a method built into Android that allows you to do this, but my first thought is recursion.

我确信Android中内置了一个允许你这样做的方法,但我首先想到的是递归。

The problem is that your listeners are in the onCreate method, which means that after they are run through, they won't repeat. In the back button listener, when you set the content view to be the home screen again, that won't set up the listeners again, that will just change the content view.

问题是您的侦听器处于onCreate方法中,这意味着在它们运行之后,它们将不会重复。在后退按钮侦听器中,当您再次将内容视图设置为主屏幕时,将不会再次设置侦听器,这只会更改内容视图。

To fix that, you would have to call the onCreate method again, once the back button is clicked, because then it would run your whole code with all the listeners from the home screen again.

要解决这个问题,一旦单击后退按钮,就必须再次调用onCreate方法,因为它会再次从主屏幕上运行整个代码与所有侦听器。

I suggest putting all of the listeners in a listeners() method, and then calling that method recursively when needed. It would need to be called in onCreate(...), as well as when the back button is clicked:

我建议将所有侦听器放在listeners()方法中,然后在需要时递归调用该方法。它需要在onCreate(...)中调用,以及单击后退按钮时:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    listeners(); // set up all the listeners for the buttons    
}

public void listeners()
{
    setContentView(R.layout.main); // return to home screen
    // sets up a listener for when the GCF main screen button is clicked.
    GCFButton.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {
            setContentView(R.layout.gcf); // change to the gcf layout
            Button back = (Button)findViewById(R.id.btnBack); // set up the back button in the gcf layout
            back.setOnClickListener(new View.OnClickListener() // put a listener on back button
            {
                public void onClick(View v) 
                {
                    listeners(); // recursively call the listeners again to 'start over'
                }
            });

            Button GCFCalculate = (Button)findViewById(R.id.btnCalculate); // set up the gcf button in the gcf layout
            GCFCalculate.setOnClickListener (new View.OnClickListener() // put listener on gcf button in gcf layout
            {
                public void onClick(View v)
                {
                    // do stuff
            }
        });
    }
});

}

I would also recommend putting the back button listener in its own method, so that it can be called every time the layout is changed.

我还建议将后退按钮侦听器放在自己的方法中,以便每次更改布局时都可以调用它。

#1


2  

You should not change a screen with setContentView(). Screens are changed in Android by starting a new Activity with startActivity(new Intent(...)) or with Fragments like recommended by Malimo (which is a bit more difficult to do but much nicer). You call two times setContentView() where one is destroying the other one.

您不应该使用setContentView()更改屏幕。通过启动一个带有startActivity的新Activity(新的Intent(...))或者像Malimo推荐的Fragments(这样做更难做但更好),在Android中更改了屏幕。你调用两次setContentView(),其中一个正在摧毁另一个。

#2


1  

in my opinion you should use fragments for your contentviews. so every fragment will be responsible for its contentview and can add listeners each time it is displayed... http://developer.android.com/guide/components/fragments.html

在我看来,你应该使用片段作为你的内容视图。因此每个片段都将负责其内容视图,并且每次显示时都可以添加监听器... http://developer.android.com/guide/components/fragments.html

#3


0  

I'm sure that there is a method built into Android that allows you to do this, but my first thought is recursion.

我确信Android中内置了一个允许你这样做的方法,但我首先想到的是递归。

The problem is that your listeners are in the onCreate method, which means that after they are run through, they won't repeat. In the back button listener, when you set the content view to be the home screen again, that won't set up the listeners again, that will just change the content view.

问题是您的侦听器处于onCreate方法中,这意味着在它们运行之后,它们将不会重复。在后退按钮侦听器中,当您再次将内容视图设置为主屏幕时,将不会再次设置侦听器,这只会更改内容视图。

To fix that, you would have to call the onCreate method again, once the back button is clicked, because then it would run your whole code with all the listeners from the home screen again.

要解决这个问题,一旦单击后退按钮,就必须再次调用onCreate方法,因为它会再次从主屏幕上运行整个代码与所有侦听器。

I suggest putting all of the listeners in a listeners() method, and then calling that method recursively when needed. It would need to be called in onCreate(...), as well as when the back button is clicked:

我建议将所有侦听器放在listeners()方法中,然后在需要时递归调用该方法。它需要在onCreate(...)中调用,以及单击后退按钮时:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    listeners(); // set up all the listeners for the buttons    
}

public void listeners()
{
    setContentView(R.layout.main); // return to home screen
    // sets up a listener for when the GCF main screen button is clicked.
    GCFButton.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {
            setContentView(R.layout.gcf); // change to the gcf layout
            Button back = (Button)findViewById(R.id.btnBack); // set up the back button in the gcf layout
            back.setOnClickListener(new View.OnClickListener() // put a listener on back button
            {
                public void onClick(View v) 
                {
                    listeners(); // recursively call the listeners again to 'start over'
                }
            });

            Button GCFCalculate = (Button)findViewById(R.id.btnCalculate); // set up the gcf button in the gcf layout
            GCFCalculate.setOnClickListener (new View.OnClickListener() // put listener on gcf button in gcf layout
            {
                public void onClick(View v)
                {
                    // do stuff
            }
        });
    }
});

}

I would also recommend putting the back button listener in its own method, so that it can be called every time the layout is changed.

我还建议将后退按钮侦听器放在自己的方法中,以便每次更改布局时都可以调用它。