【问题标题】:Change button background in Android when two activities两个活动时更改Android中的按钮背景
【发布时间】: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


【解决方案1】:

示例。

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

【讨论】:

    【解决方案2】:

    来自您的 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) 来自您的第二个 Activity():

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

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

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

    【讨论】:

    • 第二个活动出现错误。 receiveColorIntent 无法解析为变量,因此..
    • 我编辑了我的帖子并上传了完整的代码。我的朋友绝对应该有用。我现在试了10多次。它每次都有效.. :)
    • 我已经用 logcat 更新了这个问题。
    • 还是不行吗?你也可以发布你的代码 sn-p 吗?
    猜你喜欢
    • 2018-03-18
    • 2018-04-29
    • 2016-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    相关资源
    最近更新 更多