简体   繁体   English

单击按钮时如何更改图像?

[英]How do I change an image when clicking a button?

so i am trying to change an image when a button below it is clicked. 所以当我单击下面的按钮时,我试图更改图像。 I am trying to toggle the phone from normal to silent mode and have a picture change according to the state of the phone. 我试图将手机从普通模式切换到静音模式,并根据手机的状态更改图片。

I am following a book and dont know what is wrong ( I am not copy pasting cuz that creates problems). 我正在看书,不知道出了什么问题(我不是在复制粘贴cuz会造成问题)。 Good news though. 好消息。 Although the picture does not change when the phone is toggled to silent mode, When I reopen the app with the silent mode already on, the image changes to what its supposed to be when its on silent. 尽管将手机切换到静音模式后图片不会改变,但是当我在静音模式已打开的情况下重新打开应用程序时,图片会变为静音模式时的图像。 And when I toggle it back to normal mode, it works but does not change its image till I close and reopen the app and the system reads the state of the phone. 当我将其切换回正常模式时,它可以工作,但是直到我关闭并重新打开应用程序并且系统读取手机状态后,它的图像才会改变。 I have no idea whats wrong but heres my code: 我不知道怎么了,但是我的代码如下:

private AudioManager mAudioManager;
    private boolean mPhoneIsSilent;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.linear);

   mAudioManager= (AudioManager)getSystemService (AUDIO_SERVICE);
    checkIfPhoneIsSilent();
   setButtonClickListener(); 
   toggleUi();
}


private void setButtonClickListener(){

    Button toggleButton=(Button) findViewById(R.id.toggleButton);
    toggleButton.setOnClickListener(

            new View.OnClickListener(){

                public void onClick(View v){

                    if (mPhoneIsSilent){
                        mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            mPhoneIsSilent=false;
            }
            else{
                mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
            mPhoneIsSilent=true;
            }
        }

    }
            );
}



private void toggleUi() {
    ImageView imageView=(ImageView)findViewById(R.id.phone_icon);
    Drawable newPhoneImage;
    if(mPhoneIsSilent)
        newPhoneImage=getResources().getDrawable(R.drawable.mute);
    else
        newPhoneImage=getResources().getDrawable(R.drawable.unmute);
    imageView.setImageDrawable(newPhoneImage);
    setContentView(R.layout.linear);
}


    private void checkIfPhoneIsSilent()
    {
        int ringerMode=mAudioManager.getRingerMode();
        if(ringerMode==AudioManager.RINGER_MODE_SILENT)
            mPhoneIsSilent=true;
        else mPhoneIsSilent=false;
    }

and here is the XML to go along with it: 这是与之配套的XML:

       <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

 <ImageView
      android:id="@+id/phone_icon"               
      android:layout_width="wrap_content"               
      android:layout_height="wrap_content"               
      android:layout_gravity="center_horizontal"               
      android:src="@drawable/unmute" />
 <Button 
     android:id="@+id/toggleButton"         
     android:layout_width="wrap_content"         
     android:layout_height="wrap_content"          
     android:layout_gravity="center_horizontal"         
     android:text="Toggle Silent Mode"/>

</LinearLayout>

there are two mistakes in your code 您的代码中有两个错误

1. this setButtonClickListener(); 1.setButtonClickListener(); called once in your activity ie in onCreate() ! 在您的活动中调用过一次,即在onCreate() so you r button OnClickListener() will work once ! 因此,您的按钮OnClickListener()将可以工作一次!

2. inside toggleUi() don't use setContentView(R.layout.linear); 2.toggleUi()内部不要使用setContentView(R.layout.linear);

Solution for 1. 解决方案1。

use following code in your onCreate() 在您的 onCreate() 使用以下代码

Button toggleButton = (Button) findViewById(R.id.toggleButton);
        toggleButton.setOnClickListener(

        new View.OnClickListener() {

            public void onClick(View v) {

                if (mPhoneIsSilent) {
                    mAudioManager
                            .setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                    mPhoneIsSilent = false;
                    toggleUi();
                } else {
                    mAudioManager
                            .setRingerMode(AudioManager.RINGER_MODE_SILENT);
                    mPhoneIsSilent = true;
                    toggleUi();
                }
            }

        });

and remove these two lines code from onCreat() 并从onCreat()删除这两行代码

setButtonClickListener(); 
   toggleUi();

also remove your private void setButtonClickListener() completely ! 还要完全删除您的private void setButtonClickListener()


Solution for 2. 解决方案2。

your toggleUi() should be: 您的toggleUi() 应该是:

private void toggleUi() {
        ImageView imageView = (ImageView) findViewById(R.id.phone_icon);
        Drawable newPhoneImage;
        if (mPhoneIsSilent)
            newPhoneImage = getResources().getDrawable(R.drawable.edit_btn);
        else
            newPhoneImage = getResources().getDrawable(R.drawable.ic_launcher);
        imageView.setImageDrawable(newPhoneImage);

    }

Here is simple answer 这是简单的答案

ImageView imageView;
private void toggleUi() {
        imageView=(ImageView)findViewById(R.id.phone_icon);
        Drawable newPhoneImage;
        if(mPhoneIsSilent)
            imageView.setImageResource(R.drawable.mute);
        else
            imageView.setImageResource(R.drawable.unmute);
    }

and your click event will be 您的点击事件将是

private void setButtonClickListener(){ 私人void setButtonClickListener(){

    Button toggleButton=(Button) findViewById(R.id.toggleButton);
    toggleButton.setOnClickListener(

            new View.OnClickListener(){

                public void onClick(View v){

                    if (mPhoneIsSilent){
                        mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            mPhoneIsSilent=false;
            }
            else{
                mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
            mPhoneIsSilent=true;
            }
            toggleUi();
        }

    }
            );
}

You do not call toggleUi() in your OnClickListener, after you change the state of the phone. 更改电话的状态后,请勿在OnClickListener中调用toggleUi() Thus the button is not updated. 因此按钮不会更新。

Add a call to toggleUi() as last line of the onClick(View view) method of your OnClickListener . 加入到呼叫toggleUi()作为最后一行onClick(View view)你的方法OnClickListener That should help. 那应该有帮助。 And use the simplified method of dinesh sharma too. 并且也使用dinesh sharma的简化方法。

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

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