我是新来的,我一直在寻找让我的应用播放声音的方法,并在点击按钮时在后台更改图像。 该应用程序非常简单,它具有图像和按钮的相对布局。 我已经让它播放声音,但它并没有改变屏幕上的图像。
我找到了如何更改屏幕上的图像以及如何播放声音,但我无法在同一个按钮点击时同时进行这两种操作。
这里是我的布局代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="us.xago.chtmpier.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/pier"
android:scaleType="centerCrop"/>
<ImageView
android:id="@+id/alfredo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/alfredo"
android:scaleType="centerCrop"/>
<Button
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CHTM PIER"/>
</RelativeLayout>
和java
import android.support.v7.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView alfredo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alfredo = (ImageView) findViewById(R.id.alfredo);
//set invisible
alfredo.setVisibility(View.INVISIBLE);
alfredo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//show image on the center of screen
//set image
alfredo.setImageResource(R.drawable.alfredo);
// set visible
alfredo.setVisibility(View.VISIBLE);
}
});
Button one = (Button) this.findViewById(R.id.button1);
alfredo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//show image on the center of screen
//set image
// set visible
alfredo.setVisibility(View.VISIBLE);
}
});
final MediaPlayer mp = MediaPlayer.create(this, R.raw.chtmpier);
one.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mp.start();
}
});
}
}
答案 0 :(得分:0)
你必须在Button的Listener中编写你想要执行的任何内容。在这种情况下,两件事,图像可见性和音乐播放
// Search and initialize ImageView state
alfredo = (ImageView) findViewById(R.id.alfredo);
alfredo.setVisibility(View.INVISIBLE);
// Create Player
final MediaPlayer mp = MediaPlayer.create(this, R.raw.chtmpier);
// Search and initialize Button listener
Button one = (Button) this.findViewById(R.id.button1);
one.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
/* This will be executed when the button is pressed */
// Show picture
alfredo.setVisibility(View.VISIBLE);
// Play music
mp.start();
}
});
答案 1 :(得分:0)
最简单的方法是重用您的侦听器,因此您必须将其存储在变量中。之后,您可以在按钮侦听器上重用侦听器。
import android.support.v7.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView alfredo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alfredo = (ImageView) findViewById(R.id.alfredo);
//set invisible
alfredo.setVisibility(View.INVISIBLE);
final OnClickListener changeImage = new OnClickListener() {
public void onClick(View v) {
//show image on the center of screen
//set image
alfredo.setImageResource(R.drawable.alfredo);
// set visible
alfredo.setVisibility(View.VISIBLE);
}
}
alfredo.setOnClickListener(changeImage);
Button one = (Button) this.findViewById(R.id.button1);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.chtmpier);
one.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mp.start();
changeImage.onClick(v);
}
});
}
}
答案 2 :(得分:0)
要更改ImageView
并使用Button
播放声音,只需使用Button.setOnClickListener()
。您可以使用您在问题中创建的变量来执行此类操作:
// Initializes the MediaPlayer to play the sound
MediaPlayer mp = MediaPlayer.create(this, R.raw.chtmpier);
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Changes the image to another one
alfredo.setImageResource(R.drawable.alfredo); // Replace with whatever image
// Ensures it is visible
alfredo.setVisibility(View.VISIBLE);
// Plays the sound
mp.start();
}
});
希望它有所帮助!
答案 3 :(得分:0)
您的要求非常简单,您的代码应该是这样的
import android.support.v7.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView alfredo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alfredo = (ImageView) findViewById(R.id.alfredo);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.chtmpier);
Button one = (Button) this.findViewById(R.id.button1);
one.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//show image on the center of screen
//set image
alfredo.setImageResource(R.drawable.alfredo);
mp.start();
}
});
}
}