我想做一個簡單的切換靜音模式的應用程序。我得到了傻瓜書,到目前爲止沒有運氣。需要一個按鈕來改變它的文字點擊時
我想要做的是何時音頻打開我想將我的按鈕上的文本更改爲關閉(使用字符串)。當按下時,我希望它更改爲另一個字符串。但是我無法正確理解這件事。到目前爲止,我嘗試了幾種方法。有什麼建議麼 ?
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;
}
}
大幫忙謝謝。 –
我從來沒有在書中看到這一點,我將不得不記下這一點。 –