【发布时间】:2014-06-10 20:46:05
【问题描述】:
我已经搜索了很多方法来做到这一点,但没有运气。
我有两个活动,活动1有2个按钮,活动2有1个按钮,我希望当我点击活动2中的按钮时,活动1中的第一个按钮会改变其背景。
有可能吗?
非常感谢。
这里是活动 1
public class MainActivity extends ActionBarActivity {
private Vibrator myVib;
Intent receive = null;
String bg_color;
String text_color;
Button Q1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
myVib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
receive = getIntent();
bg_color = receive.getStringExtra("bg_color");
Q1 = (Button) findViewById(R.id.button1);
Button Q2 = (Button) findViewById(R.id.button2);
Q1.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(MainActivity.this, Q01.class);
MainActivity.this.startActivity(myIntent);
myIntent.putExtra("bg_color", "#333333");
myIntent.putExtra("text_color", "#cccccc");
myVib.vibrate(50);
}
});
Q2.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(MainActivity.this, Q02.class);
MainActivity.this.startActivity(myIntent);
myVib.vibrate(50);
}
});
}
public void receiveColor() {
if (bg_color != null && text_color != null) {
q1.setBackgroundColor(Color.parseColor(bg_color));
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
receiveColor();
}
}
这是我的 Q01 活动(第二个)
public class Q01 extends ActionBarActivity {
private Vibrator myVib;
Button ok;
Intent receiveColorIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myVib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
TextView type = (TextView) findViewById(R.id.type);
TextView type1 = (TextView) findViewById(R.id.type1);
TextView type2 = (TextView) findViewById(R.id.type2);
TextView type3 = (TextView) findViewById(R.id.type3);
final Button value = (Button) findViewById(R.id.value);
TextView value1 = (TextView) findViewById(R.id.value1);
TextView value2 = (TextView) findViewById(R.id.value2);
TextView value3 = (TextView) findViewById(R.id.value3);
ok = (Button) findViewById(R.id.ok);
type.setText(R.string.type01_);
type1.setText(R.string.type01_1);
type2.setText(R.string.type01_2);
type3.setText(R.string.type01_3);
value.setText(R.string.value01_);
value1.setText(R.string.value01_1);
value2.setText(R.string.value01_2);
value3.setText(R.string.value01_3);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Q01.this, MainActivity.class);
i.putExtra("bg_color", "#FFF");
i.putExtra("text_color", "#ccc");
startActivity(i);
}
});
}
public void ReceiveColor() {
receiveColorIntent = getIntent();
if (receiveColorIntent != null) {
String bg_color = receiveColorIntent.getStringExtra("bg_color");
ok.setBackgroundColor(Color.parseColor(bg_color));
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
ReceiveColor();
}
}
这里是目录06-10 23:45:21.164: E/AndroidRuntime(9506): FATAL EXCEPTION: main
06-10 23:45:21.164: E/AndroidRuntime(9506): Process: com.example.quiz, PID: 9506
06-10 23:45:21.164: E/AndroidRuntime(9506): java.lang.RuntimeException: Unable to resume activity {com.example.quiz/com.example.quiz.q01}: java.lang.NullPointerException
06-10 23:45:21.164: E/AndroidRuntime(9506): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2788)
06-10 23:45:21.164: E/AndroidRuntime(9506): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2817)
06-10 23:45:21.164: E/AndroidRuntime(9506): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
06-10 23:45:21.164: E/AndroidRuntime(9506): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-10 23:45:21.164: E/AndroidRuntime(9506): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-10 23:45:21.164: E/AndroidRuntime(9506): at android.os.Handler.dispatchMessage(Handler.java:102)
06-10 23:45:21.164: E/AndroidRuntime(9506): at android.os.Looper.loop(Looper.java:136)
06-10 23:45:21.164: E/AndroidRuntime(9506): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-10 23:45:21.164: E/AndroidRuntime(9506): at java.lang.reflect.Method.invokeNative(Native Method)
06-10 23:45:21.164: E/AndroidRuntime(9506): at java.lang.reflect.Method.invoke(Method.java:515)
06-10 23:45:21.164: E/AndroidRuntime(9506): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-10 23:45:21.164: E/AndroidRuntime(9506): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-10 23:45:21.164: E/AndroidRuntime(9506): at dalvik.system.NativeStart.main(Native Method)
06-10 23:45:21.164: E/AndroidRuntime(9506): Caused by: java.lang.NullPointerException
06-10 23:45:21.164: E/AndroidRuntime(9506): at android.graphics.Color.parseColor(Color.java:209)
06-10 23:45:21.164: E/AndroidRuntime(9506): at com.example.cahllenge.Day01.ReceiveColor(q01.java:80)
06-10 23:45:21.164: E/AndroidRuntime(9506): at com.example.cahllenge.Day01.onResume(q01.java:90)
06-10 23:45:21.164: E/AndroidRuntime(9506): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
06-10 23:45:21.164: E/AndroidRuntime(9506): at android.app.Activity.performResume(Activity.java:5310)
06-10 23:45:21.164: E/AndroidRuntime(9506): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2778)
【问题讨论】:
-
但你不会看到它发生.. 除非你以额外的意图开始活动,否则不可能做到这一点
-
因此,除非您想在单击活动 2 上的按钮时查看活动 1,否则只需单击活动 2 上的按钮并使用意图并将额外的作为整数传递(一些颜色的十六进制代码)和在活动一上,onResume(),从活动 2 中获取先前创建的意图的意图和结果,然后设置按钮背景。所以基本上最初当应用程序启动时,您的按钮不会发生任何事情,但是在从其他活动中单击按钮时,您的按钮 bg 会改变。
-
如果我将Activity2中的按钮替换为Checkbox会怎样?
-
如果是复选框,显然你不想去上一个活动检查颜色是否改变。但是,如果您愿意,请在选中复选框时启动活动并传递您想要的值。
-
我不想开始活动。我想要做的是,当用户完成测验并单击确定时,在主要活动中它显示用户完成了测验 A 等等..
标签: android button android-activity