两个活动时在Android中更改按钮背景

时间:2014-06-10 20:46:06

标签: android button android-activity

我已经搜索了很多方法,但没有运气。

我有两个活动,Activity1有2个按钮,Activity2有1个按钮,我希望当我点击Activity2中的按钮时,Activity1中的第一个按钮会改变它的背景。

有可能吗?

非常感谢。

这是Activity1

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();

}

}

这里是catlog 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 个答案:

答案 0 :(得分:0)

样品。

static int color=1;
 btn1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                Intent i = new Intent(MainActivity.this,Second.class); 
                i.putExtra("color", color);
                startActivity(i);

            }
        });

然后在第二个

int color2;
color2= intent.getIntExtra("color", color2);

 if(color2==1);
// change button color

答案 1 :(得分:0)

来自您的FirstActivity:

点击按钮:

btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                i.putExtra("bg_color", "#333333");
                i.putExtra("text_color", "#cccccc");
                startActivity(i);

            }
        });

关于第二项活动:

public void ReceiveColor(){

        receiveColorIntent = getIntent();
        if(receiveColorIntent!=null){
        String bg_color = receiveColorIntent.getStringExtra("bg_color");
        String text_color = receiveColorIntent.getStringExtra("text_color");
        btn1.setBackgroundColor(Color.parseColor(bg_color));
        btn1.setTextColor(Color.parseColor(text_color));
        }

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        ReceiveColor();

    }

以下是完整的代码:

1)您将进入第二项活动的主要活动:

public class MainActivity extends Activity {

    Intent receive = null;
    Button btn1;
    Button btn2;
    String bg_color;
    String text_color;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        receive = getIntent();
        bg_color = receive.getStringExtra("bg_color");
        text_color = receive.getStringExtra("text_color");

        btn1 = (Button) findViewById(R.id.button1);
        btn2 = (Button) findViewById(R.id.button2);

        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                i.putExtra("bg_color", "#333333");
                i.putExtra("text_color", "#cccccc");
                startActivity(i);

            }
        });

    }

    public void receiveColor() {

        if (bg_color != null && text_color != null) {
            btn2.setBackgroundColor(Color.parseColor(bg_color));
            btn2.setTextColor(Color.parseColor("#cccccc"));
        }

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        receiveColor();

    }

}

2)来自您的第二个活动():

public class SecondActivity extends Activity {

    Button btn1;
    Intent receiveColorIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondlayout);

        btn1 = (Button) findViewById(R.id.sbutton1);

        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(SecondActivity.this, MainActivity.class);
                i.putExtra("bg_color", "#333333");
                i.putExtra("text_color", "#ccc");
                startActivity(i);

            }
        });

    }

    public void ReceiveColor() {

        receiveColorIntent = getIntent();
        if (receiveColorIntent != null) {
            String bg_color = receiveColorIntent.getStringExtra("bg_color");
            String text_color = receiveColorIntent.getStringExtra("text_color");
            btn1.setBackgroundColor(Color.parseColor(bg_color));
            btn1.setTextColor(Color.parseColor(text_color));
        }

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        ReceiveColor();

    }

}

我很确定这是一个解决方案,它对我很有用。你可以尝试这个,看看它适合你。

至少你现在可以这样做。而且肯定还有其他解决方案。但这对我有用.. :))