简体   繁体   English

按下另一个活动中的按钮时更改按钮颜色

[英]Change button color when a button in another activity is pressed

I'm new in Android programming and I still have sometimes small problems.我是 Android 编程的新手,有时我仍然会遇到一些小问题。 My problem currently is the following:我目前的问题如下:

I have 2 activities with just one button in each.我有 2 个活动,每个活动只有一个按钮。 The button of the first activity is opening the second activity.第一个活动的按钮是打开第二个活动。 But when I press the button in the second activity, the Text in the Button of the first activity should change to "Hello" and the color should be red.但是当我在第二个活动中按下按钮时,第一个活动的按钮中的文本应更改为“Hello”,颜色应为红色。

I have managed to change the text but not the color.我设法更改了文本,但没有更改颜色。 Could someone help me please?有人可以帮我吗?

My Code:我的代码:

First activity:第一项活动:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

Button placeHolder;
Intent intent;

public void button0(View v){

    intent = new Intent(getApplicationContext(), MainActivity2.class);
    startActivity(intent);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    placeHolder = findViewById(R.id.button);

    placeHolder.setText(getIntent().getStringExtra("message"));
}
}

The code of second activity:第二个活动的代码:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity2 extends AppCompatActivity {


public void buttonOnClick(View v){

    Intent intent=new Intent(MainActivity2.this, MainActivity.class);
    intent.putExtra("message", "Hello");
    startActivity(intent);
}

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

Add this line of code in your first activity where you set your text to your button:在您将文本设置为按钮的第一个活动中添加这行代码:

To change background color:要更改背景颜色:

placeHolder.setBackgroundColor(Color.parseColor("#FF0000"));

To change text color:要更改文本颜色:

placeHolder.setTextColor(Color.parseColor("#FF0000"));

Usage: MainActivity.java用法: MainActivity.java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

Button placeHolder;
Intent intent;

public static final int REQUEST_CODE = 101;

public void button0(View v){

    intent = new Intent(getApplicationContext(), MainActivity2.class);
    startActivityForResult(intent, REQUEST_CODE);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    placeHolder = findViewById(R.id.button);
}

    // Call Back method  to get the Message form other Activity  
    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data){  
    super.onActivityResult(requestCode, resultCode, data);  

    // check if the request code is same as what is passed
  
    if(requestCode == REQUEST_CODE) {  
       placeHolder.setText(data.getStringExtra("message"));
       placeHolder.setTextColor(Color.parseColor("#FF0000"));
    }  

}  

MainActivity2:主要活动2:


package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity2 extends AppCompatActivity {


public void buttonOnClick(View v){

    Intent intent=new Intent();
    intent.putExtra("message", "Hello");
    setResult(MainActivity2.REQUEST_CODE, intent);
    finish();
}

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

you can use startActivityforResult() insted of startActivity() and then when you come back from second class to the first class then set red color tor your button onActivityResult() function thanks你可以使用 startActivityforResult() insted of startActivity() 然后当你从第二个 class 回到第一个 class 然后设置红色按钮 onActivityResult() ZC1C425268E68385D14AB5074C17ZA

You can use GreenRobot.您可以使用绿色机器人。 Then you can do anything from other activities.然后你可以从其他活动中做任何事情。 Here is the link Event Bus这是链接事件总线

Second Activity:第二个活动:

Intent intent=new Intent(MainActivity2.this, MainActivity.class);
    intent.putExtra("message", "Hello");
    intent.putExtra("color", -65536); //-65536 is color RED int value
    startActivity(intent);

First Activity第一个活动

placeHolder.setText(getIntent().getStringExtra("message"));
placeHolder.setBackgroundColor(getIntent().getStringExtra("color",0)); // 0 is default value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM