我的应用有 2 个活动:
MainActivity 有一个启动 SubActivity 的按钮,这个按钮没有设置为透明的。
SubActivity 有一个通过 setAlpha() 透明的按钮。
我的问题是有时,当我通过按返回按钮从 SubActivity 返回 MainActivity 时,MainActivity 的按钮变得与 SubActivity 的按钮一样透明。如果我退出应用程序并再次打开它,则会经常出现此问题。
我还尝试在每个活动的 onResume() 中为 button1 设置 alpha 255,为 button2 设置 120,但它不起作用。有时 button1 变得透明,有时 button2 不透明。
我上传 MainActivity.java:
package com.example.buttondemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
startActivity(new Intent(this, SubActivity.class));
break;
default:
break;
}
}
}
子活动.java:
package com.example.buttondemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
public class SubActivity extends Activity {
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
button2 = (Button) findViewById(R.id.button2);
button2.getBackground().setAlpha(120);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sub, menu);
return true;
}
}
\res\values\strings.xml:
<string name="app_name">ButtonDemo</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_sub">SubActivity</string>
<string name="button1_label">button1</string>
<string name="button2_label">button2</string>
\res\layout\activity_main.xml;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button1_label"
android:background="@drawable/button_selector" />
</RelativeLayout>
\res\layout\activity_sub.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SubActivity" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button2_label"
android:background="@drawable/button_selector" />
</RelativeLayout>
\res\drawable\button_selector.xml:
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/pressed_button" /> <!-- pressed -->
<item
android:state_focused="true"
android:drawable="@drawable/pressed_button" /> <!-- focused -->
<item
android:drawable="@drawable/default_button" /> <!-- default -->
</selector>