如何在android studio中使用按钮更改图像?
我正在做一个项目,需要我用按钮onclick或其他按钮方法(如果有的话)更改同一表单内容页中的图像。我通常使用visual studio用C编程,但我不习惯java,因此非常感谢您的帮助。这是一个图像转换器示例代码 Xml-如何在android studio中使用按钮更改图像?,android,Android,我正在做一个项目,需要我用按钮onclick或其他按钮方法(如果有的话)更改同一表单内容页中的图像。我通常使用visual studio用C编程,但我不习惯java,因此非常感谢您的帮助。这是一个图像转换器示例代码 Xml- <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.and
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/changeBtn"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginStart="151dp"
android:layout_marginLeft="151dp"
android:layout_marginEnd="172dp"
android:layout_marginRight="172dp"
android:layout_marginBottom="4dp"
android:onClick="change"
android:text="Change"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="456dp"
android:layout_height="632dp"
android:scaleType="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/cats01" />
</android.support.constraint.ConstraintLayout>
在您的setOnClicklistener of按钮中,编写更改要更改的imageview的代码,例如,如果您的imageview名称为ivIcon,则应编写:ivIcon.setImageResourceR.drawable.your_imagePlease不要使用[android studio]标记来回答一般android编程问题。正确的标签是[android]。[android studio]标签用于回答特定于该IDE功能的问题。还请回答您的问题,以准确解释您在实现所述设计时遇到的问题。这里已经有很多例子说明了如何满足您的要求。我们需要知道你在哪些方面遇到了问题。我做了我的问题是因为它是一个洞,因为我不知道该做什么,我不知道我是否必须在代码中这样做,所以我无法具体说明,但穆罕默德·哈迪为我完美地回答了这个问题,我想。
public class MainActivity extends AppCompatActivity {
private int i =0;
public void change(View view)
{
ImageView imageView2 = (ImageView)findViewById(R.id.imageView2);
imageView2.setImageResource(R.drawable.cats01); // this function
changes the image
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}