繁体   English   中英

按下时改变按钮的图像,释放时使按钮的图像不改变

[英]Change image of button when pressed, make image of button not change when released

当我按下按钮图像改变,但我的问题是当我释放按钮时,图像再次改变(不保留图像)。 我是 Java 编程的新手。

我的布局:

 <Button
    android:background="@drawable/volume"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"/>

这是我在 drawable 中的 XML:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
    android:drawable="@drawable/volume_off" /> <!-- pressed -->

<item android:drawable="@drawable/volume_on" /> <!-- default -->

在您的情况下,您想更改音量指示器,我认为您应该像这样以编程方式进行。

  //At the top of your activity,declare a global variable
  boolean isVolumeOn = true;

  volumeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //check your volume status
                    if(isVolumeOn){
                        view.setBackgroundResource(R.drawable.volume_off);
                        isVolumeOn = false;
                        //Turn off your volume here

                    }else{
                        view.setBackgroundResource(R.drawable.volume_on);
                        isVolumeOn = true
                        //Turn on your volume here

                    }
                }
            });

您想从选择器中删除默认图像

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/volume_off" /> <!-- pressed -->
</selector>

删除这一行<!--<item android:drawable="@drawable/volume_on" /> remove default-->

当按钮状态改变时,它被设置为默认值。

<Button
android:background="@drawable/volume"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="@drawable/volume_on"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>

如果您想在单击按钮时永久更改按钮的图像,可以方便地在按钮的 onClick 空隙内以编程方式进行。 例如如果

image1: R.drawable.image_first image2: R.drawable.image_second

在您的 XML 布局文件中,您有:

 <Button
    android:background="@drawable/volume"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:onClick="click"/>

在活动中,您可以执行以下操作:

public Class myActivity extends Activity{

Button mButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myactivity_layout);
    mButton = (Button) findViewById(R.id.button);
}

public void click(View v){
   mButton.setBackgroundResource(R.drawable.image_second);
}

}

如果您想再次单击后返回上一张图像,只需保存按钮的状态(布尔值单击 = true/false)并在 public void click 中放置一个 if 语句。

在我的 content_main.xml

 <Button
    android:id="@+id/button"
    android:background="@drawable/volume_on"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:onClick="click" />

在我的 MainActivity.java

public Class myActivity extends AppCompatActivity{

按键音量; 布尔值 isPressed=false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myactivity_layout);
    volume = (Button) findViewById(R.id.button);
}

public void click(View v){
if (isPressed) {
    volume.setBackgroundResource(R.drawable.volume_off);
    Toast.makeText(this, "Volume Off",
            Toast.LENGTH_SHORT).show();
}else{
    volume.setBackgroundResource(R.drawable.volume_on);
    Toast.makeText(this, "Volume On",
            Toast.LENGTH_SHORT).show();
}
    isPressed=!isPressed;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM