Android的静音模式切换静音、模式、Android

2023-09-05 11:02:49 作者:涐的野心就是占有沵

我开始阅读的Andr​​oid傻瓜,我试图让我的第一个应用程序,是静音模式切换。

I started reading Android for dummies and I am trying to make my first app that is Silent Mode Toggle.

现在我面临的问题是:

在布局,我需要这样的东西只有一个按键应该在那里。如果手机是在无声的文字应为静默模式激活,当手机处于正常模式正常模式激活。

In the layout, I need something like only one button should be there. If the phone is on silent the text should read "Silent Mode Activated" and when the phone is in normal mode "Normal Mode Activated".

这应该问他/她想要激活振动模式或没有用户。

It should ask the user that He/She wants to activate vibration mode or not.

下面是我的code:

XML布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff" >    

    <ImageView
        android:id="@+id/phone_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="7dp"
        android:src="@drawable/phone_on" />

    <Button
        android:id="@+id/silent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="35dp"
        android:text="@string/silent_off" />

</LinearLayout>

的Java code

public class MainActivity extends Activity {
    private AudioManager mAudioManager;
    private boolean mPhoneIsSilent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        super.onCreate(savedInstanceState);
        mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
        checkIfPhoneIsSilent();
        setButtonClickListener();
        Log.d("SilentModeApp", "This is a test");
    }    

    private void setButtonClickListener() {
        Button toggleButton = (Button)findViewById(R.id.silent);
        toggleButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (mPhoneIsSilent) {
                    //change back to normal mode
                    mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                    mPhoneIsSilent = false;
                }
                else
                {
                    //change to silent mode
                    mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                    mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                    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){
            newPhoneImage = 
                    getResources().getDrawable(R.drawable.phone_silent);
        }
        else
        {
            newPhoneImage = 
                    getResources().getDrawable(R.drawable.phone_on);

        }
        imageView.setImageDrawable(newPhoneImage);
    }

    @Override      
    protected void onResume() {
        super.onResume();
        checkIfPhoneIsSilent();
        toggleUi();
    }
}

更新

现在我删除mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);,其从正常模式将振动模式

Now I removed "mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); " and its going to vibration mode from normal mode.

我需要与布局的事情有所帮助。

I need help with the layout thing.

我需要3个按钮和一个文本状态的文字 - 我知道如何创建按钮和文本消息,是什么我不知道的是如何将它与Java的集成

I need 3 buttons and a text status text - I know how to create buttons and text message, what I dont know is how to integrate it with java.

我需要的,如果手机是在正常模式下应该有下面的图片上面的按钮的状态消息:(XXX模式被激活),其中XXX为静音,普通或振动

I need if the phone is in normal mode there should be a status message below image above buttons : (XXX MODE IS ACTIVATED ) where XXX is Silent, Normal Or Vibrate.

的,如果手机处于正常模式,则应该只有2个按钮:切换到静音模式,并切换到振动模式

The if the phone is in normal mode then there should be only 2 buttons : Switch to silent mode and Switch to vibration mode.

和同样的事情,与其他模式了。

And the same thing with other modes too.

推荐答案

这听起来像你想一个TextView,说是否静音模式/心不是激活?即靠近按钮的文字只是一个小一点,显示状态?而你的按钮来动态显示/ dissapear呢?

It sounds like you want a textview that says whether silent mode is/isnt activated? ie just a small bit of text near a button, that shows status? And your buttons to dynamically appear/dissapear as well?

TextView tv = (TextView)findviewbyid(R.id.relevanttextview); //text above button/wherever
Button bt = (Button)findviewbyid(R.id.relevantbutton); //button to make appear/dissapear 

if(vibrate is on){
tv.setText("vibrate is on");
bt.setVisibility(View.VISIBLE); //make button appear
}

if(normal mode is on){
tv.setText("normal");
bt.setVisibility(View.INVISIBLE); //make button dissapear
}

等等...

etc...