Java 显示对 x 次按压的操作的代码?

Java code to show action on x amount of presses?

我有一个 iOS 应用程序,我以前开发过它,我喜欢它使用的功能,并希望以某种方式让它在我拥有的一些 Android 代码上运行。

在我的MainActivity顶部我有

SharedPreferences sharedpreferences;

public static final String MyPREFERENCES = "nShownLobby";

在我的 onCreate 我有

sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

我正在调用这个方法

changeTextButton.setOnClickListener(new View.OnClickListener() {
    
    @Override
    public void onClick(View v){
        long nb_shown_lobby = PreferenceManager.getDefaultSharedPreferences(MainActivity.this).getLong("nShownLobby", 0); 
        ++nb_shown_lobby; 
        if ((nb_shown_lobby % 20) == 0) { 
            this.turnAround(); 
        }
        int random = (int) (Math.random() * manyDifferentStrings.length);
        if (random == oldVaue) {
            random = (int) (Math.random() * manyDifferentStrings.length);
        }
        changingText.setText(manyDifferentStrings[random]);
        oldVaue = random;
        try {
            mySound.start();
        } catch (NullPointerException e) {
            mySound = MediaPlayer.create(MainActivity.this, R.raw.sound);
        }
    }

    private void turnAround() {
        //Do something here
        Log.i("Do Something ", "");
    }
});

目的是每按 20 次后,将调用 turnAround() 方法,但它不会...它只是被忽略了 - 我猜我错过了什么?

**According to your question your code should be like this**

private SharedPreferences sharedpreferences;
private SharedPreferences.Editor editor;
private static final String MyPREFERENCES = "nShownLobby";

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

    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    editor = sharedpreferences.edit();

    changeTextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){


            int nb_shown_lobby = sharedpreferences.getInt("nShownLobby", 0);
            if ((nb_shown_lobby % 20) == 0) {
                turnAround();
            }

            nb_shown_lobby += 1;
            editor.putInt("nShownLobby", nb_shown_lobby);
            editor.commit();

            int random = (int) (Math.random() * manyDifferentStrings.length);
            if (random == oldVaue) {
                random = (int) (Math.random() * manyDifferentStrings.length);
            }
            changingText.setText(manyDifferentStrings[random]);
            oldVaue = random;

            try {
                mySound.start();
            } catch (NullPointerException e) {
                mySound = MediaPlayer.create(MainActivity.this, R.raw.sound);

            }

        }

    });


}

//created this in activity, not in the button onclick
private void turnAround() {

    //Do something here
    Log.i("Do Something ", "");
}