Android 返回活动时,清除后退操作

Android 返回活动时,清除后退操作,android,android-intent,Android,Android Intent,在我的主要活动中,当我点击时,我有一个登录按钮,它是“更改此按钮的背景颜色”并将我带到登录活动 // Button SignInActivity final Button signIn = (Button) findViewById(R.id.btn_sign_in); signIn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View

在我的主要活动中,当我点击时,我有一个登录按钮,它是“更改此按钮的背景颜色”并将我带到登录活动

// Button SignInActivity
    final Button signIn = (Button) findViewById(R.id.btn_sign_in);
    signIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            signIn.setBackgroundColor(getResources().getColor(R.color.colorAccent));
            Intent intent = new Intent(MainActivity.this, SignInActivity.class);
            startActivity(intent);
        }
    });
我的问题是,当我回到MainActivity时,我发现SignIn按钮的背景是colorAccent。
是否有清除背景色的解决方案?

您可以覆盖“onResume()”方法,其中需要将按钮的“setbackgroundColor”设置为默认颜色

@Override
    public protected onResume(){
          signIn.setBackgroundColor(getResources().getColor(R.color.yourcolor));
    }

您可以重写“onResume()”方法,其中需要将按钮的“setbackgroundColor”设置为默认颜色

@Override
    public protected onResume(){
          signIn.setBackgroundColor(getResources().getColor(R.color.yourcolor));
    }
你可以这样做

首先找到按钮的默认背景,如下所示

Drawable d = button.getBackground();
如果您再次需要默认背景,请使用此选项

按钮。可缩进式拉深(d)onResume()方法中的code>

使用这些行管理代码。

您可以这样做

首先找到按钮的默认背景,如下所示

Drawable d = button.getBackground();
如果您再次需要默认背景,请使用此选项

按钮。可缩进式拉深(d)onResume()方法中的code>


使用这些行管理代码。

您可以创建一个
选择器
,并将其分配给xml中的按钮。例如:-

在drawable文件夹中创建一个名为
button\u background.xml
的文件,并复制粘贴下面的选择器代码

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

        <solid android:color="@color/buttonBackgroundPressed"/>
        <stroke android:width="1dp" android:color="@color/buttonBorderColor"/>
    </shape>
</item>
<item android:state_focused="true">
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

        <solid android:color="@color/buttonBackgroundPressed"/>
        <stroke android:width="1dp" android:color="@color/buttonBorderColor"/>
    </shape>
</item>
<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

        <solid android:color="@color/buttonBackgroundNormal"/>
        <stroke android:width="1dp" android:color="@color/buttonBorderColor"/>
    </shape>
</item>

您可以创建一个
选择器
,并将其分配给xml中的按钮。例如:-

在drawable文件夹中创建一个名为
button\u background.xml
的文件,并复制粘贴下面的选择器代码

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

        <solid android:color="@color/buttonBackgroundPressed"/>
        <stroke android:width="1dp" android:color="@color/buttonBorderColor"/>
    </shape>
</item>
<item android:state_focused="true">
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

        <solid android:color="@color/buttonBackgroundPressed"/>
        <stroke android:width="1dp" android:color="@color/buttonBorderColor"/>
    </shape>
</item>
<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

        <solid android:color="@color/buttonBackgroundNormal"/>
        <stroke android:width="1dp" android:color="@color/buttonBorderColor"/>
    </shape>
</item>

在可绘制文件夹中创建自定义按钮背景可绘制,如下所示

button_background.xml


在可绘制文件夹中创建自定义按钮背景可绘制,如下所示

button_background.xml


谢谢你的回复谢谢你的回复
final Button signIn = (Button) findViewById(R.id.btn_sign_in);
    signIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //signIn.setBackgroundColor(getResources().getColor(R.color.colorAccent));
            Intent intent = new Intent(MainActivity.this, SignInActivity.class);
            startActivity(intent);
        }
    });