我正在尝试做一个简单的切换静音模式应用程序。我得到了傻瓜书,到目前为止还没有运气。
我想要做的是当音频打开时,我想将按钮上的文本更改为关闭(使用字符串)。当按下时,我希望它更改为另一个字符串。但我不能把这件事做好。到目前为止,我已经尝试了几种方法。有什么建议么 ?
package com.example.silentmodetoggle;
import android.os.Bundle;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class TSMMainActivity extends Activity {
//Instantiate an AudioManager to manage the ringer state
private AudioManager mAudioManager;
private boolean mPhoneIsSilent;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tsmmain);
mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
checkIfPhoneIsSilent();
setButtonClickListener();
//does this even work?
TextView textView=(TextView)findViewById(R.string.silence_phone);
}//onCreate
private void setButtonClickListener()
{
Button toggleButton = (Button) findViewById(R.id.toggleButton);
toggleButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (mPhoneIsSilent)
{ //set to normal mode
mAudioManager
.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
mPhoneIsSilent = false;
}
else
{
mAudioManager
.setRingerMode(AudioManager.RINGER_MODE_SILENT);
mPhoneIsSilent = true;
}
//now toggle the UI again
toggleUi();
}
});
}
private void checkIfPhoneIsSilent()
{
int ringerMode = mAudioManager.getRingerMode();
if (ringerMode == AudioManager.RINGER_MODE_SILENT)
{
mPhoneIsSilent = true;
}
else
{
mPhoneIsSilent = false;
}
}
private void toggleUi()
{
ImageView imageView = (ImageView) findViewById(R.id.phone_icon);
Drawable newPhoneImage;
if (mPhoneIsSilent)
{
//i think i should put it here.
newPhoneImage = getResources().getDrawable(R.drawable.phone_silent);
}
else
{
newPhoneImage = getResources().getDrawable(R.drawable.phone_on);
}
imageView.setImageDrawable(newPhoneImage);
}
private void textView(int silencePhone) {
// TODO Auto-generated method stub
}
@Override
protected void onResume()
{
super.onResume();
checkIfPhoneIsSilent();
toggleUi();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tsmmain, menu);
return true;
}
}