片段剂量的背景在广播接收上不改变

时间:2017-01-10 10:02:02

标签: android android-fragments drawable

我正在尝试更改广播接收片段的背景,但它不会改变。 我想要做的是当用户点击它广播的按钮时,为了改变片段的背景 enter image description here  菜单类:我实现了片段监听器,这是实现函数的代码

public class menu extends Activity implements Setting_Fragment.Setting_FragmentListener {
View frag;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu_activity);
    //hide fragment
    frag = findViewById(R.id.fragment);
    frag.setVisibility(View.GONE);


    }

public void setFragment(View view){
    frag.setVisibility(View.VISIBLE);
}

@Override
public void closefragment(String txt) {
    if(txt=="close"){
        frag.setVisibility(View.INVISIBLE);
    }
}

@Override
public void broadcolor(String col) {
    Intent i = new Intent();
    Bundle b=new Bundle();
        b.putString("col",col);
        i.putExtras(b);
        i.setAction("com.example.asus.memory");
        i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        sendBroadcast(i);
}}

这个片段类:

    Setting_FragmentListener commander;
    public interface Setting_FragmentListener{
        public void closefragment(String txt);
        public void broadcolor(String col);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.setting_fragment, container,false);
        final Button button = (Button) v.findViewById(R.id.close);
        final Button yellow = (Button) v.findViewById(R.id.yellow);
        final Button pink = (Button) v.findViewById(R.id.pink);
        final Button blue = (Button) v.findViewById(R.id.blue);
        button.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                ButtonClicked(v);
            }
        });
        pink.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                broadpink(v);
            }
        });
        yellow.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                broadyel(v);
            }
        });
        blue.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                broadbl(v);
            }
        });
        return v;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try{
            commander=(Setting_FragmentListener) activity;
        }catch(Exception e){}
    }

    public void ButtonClicked(View v){
        commander.closefragment("close");
    }
    public void broadpink(View v){
        commander.broadcolor("1");
    }
    public void broadyel(View v){
        commander.broadcolor("2");
    }
    public void broadbl(View v){
        commander.broadcolor("3");
    }
        public void color(String col){
        if (col=="1"){
            int Rcolor = Color.argb(255, 240,118,105);
            Drawable d= (Drawable)getResources().getDrawable(R.drawable.shape);
            getView().setBackground(d);
        }
        if (col=="2"){
            int Rcolor = Color.argb(255, 254,177,1);
            getView().setBackgroundColor(Rcolor);
        }
        if (col=="3"){
            int Rcolor = Color.argb(255, 107,88,193);
            getView().setBackgroundColor(Rcolor);
        }

    }

}

这是我的接收器类

    Setting_Fragment sf = new Setting_Fragment();
Win_Fragment wf = new Win_Fragment();
Lose_Fragment lf = new Lose_Fragment();
public MyReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {
    Bundle i =intent.getExtras();
    String  color = intent.getStringExtra("col");
    Toast.makeText(context,"toast",Toast.LENGTH_SHORT).show();
    sf.color(color);
    wf.color(color);
    lf.color(color);
}

0 个答案:

没有答案