英文:
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:
<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);
}
答案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("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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论