点击后如何在Android中隐藏按钮

huangapple go评论80阅读模式
英文:

How to hide the button after clicking in Android

问题

以下是您要翻译的内容:

程序运行良好。问题在于您需要点击两次才能隐藏它。

如果我只想要一次点击,播放按钮就消失了怎么办?我的代码哪里有问题。

布局:

<Button
    android:id="@+id/button111"
    android:layout_width="75dp"
    android:layout_height="80dp"
    android:foregroundGravity="center_vertical|center|center_horizontal"
    android:onClick="playSong"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:background="@drawable/play" />

Java:

public void playSong(View v) {
    this.songIntent = new Intent(Main4Activity.this, BackgroundSoundService.class);
    this.songIntent.putExtra("song", "suara_" + randomResult);
    final Button myButton = (Button) findViewById(R.id.button111);
    myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            myButton.setVisibility(View.GONE);
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean(SHARED_KEY_BUTTON_HIDE, true);
            editor.apply();
        }
    });
    startService(this.songIntent);
}
英文:

The program is running well. The problem is that you have to click 2 times before you can hide it.

What if I want just one click and the play button disappears? Where is my code wrong.

Layout:

&lt;Button
        android:id=&quot;@+id/button111&quot;
        android:layout_width=&quot;75dp&quot;
        android:layout_height=&quot;80dp&quot;
        android:foregroundGravity=&quot;center_vertical|center|center_horizontal&quot;
        android:onClick=&quot;playSong&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        android:background=&quot;@drawable/play&quot; /&gt;

Java:

 public void playSong(View v) {
        this.songIntent = new Intent(Main4Activity.this, BackgroundSoundService.class);
        this.songIntent.putExtra(&quot;song&quot;, &quot;suara_&quot; + randomResult);
        final Button myButton = (Button) findViewById(R.id.button111);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myButton.setVisibility(View.GONE);
                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                SharedPreferences.Editor editor = preferences.edit();
                editor.putBoolean(SHARED_KEY_BUTTON_HIDE, true);
                editor.apply();
            }
        });
        startService(this.songIntent);
    }

答案1

得分: 1

你需要将处理新的onClickListener隐藏的代码移到playSong方法中。

public void playSong(View v) {

    this.songIntent = new Intent(Main4Activity.this, BackgroundSoundService.class);
    this.songIntent.putExtra("song", "suara_" + randomResult);
    final Button myButton = (Button) findViewById(R.id.button111);

    myButton.setVisibility(View.GONE);
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor editor = preferences.edit();
    editor.putBoolean(SHARED_KEY_BUTTON_HIDE, true);
    editor.apply();

    startService(this.songIntent);
}
英文:

You need to move the code which handles the hiding out of the new onClickListener and put it right in the playSong method.

public void playSong(View v) {

    this.songIntent = new Intent(Main4Activity.this, BackgroundSoundService.class);
    this.songIntent.putExtra(&quot;song&quot;, &quot;suara_&quot; + randomResult);
    final Button myButton = (Button) findViewById(R.id.button111);

    myButton.setVisibility(View.GONE);
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor editor = preferences.edit();
    editor.putBoolean(SHARED_KEY_BUTTON_HIDE, true);
    editor.apply();

    startService(this.songIntent);
}

huangapple
  • 本文由 发表于 2020年8月19日 15:52:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63482372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定