简体   繁体   中英

How to set borders of buttons in activity from fragment

I have this method:

public void ButtonBorder(Button button1, Button button2, Button button3, Button button4) {
    button1.setBackgroundResource(R.color.Transparent);
    button2.setBackgroundResource(R.drawable.bottom_btn_border);
    button3.setBackgroundResource(R.drawable.all_border);
    button4.setBackgroundResource(R.drawable.all_border);
}

and I call it in my fragments onItemClick() method like this:

@Override
public void onItemClick(int position) {

    Button button1= ((MainActivity)getActivity()).button1
    Button button2= ((MainActivity)getActivity()).button2;
    Button button3= ((MainActivity)getActivity()).button3;
    Button button4=((MainActivity)getActivity()).button4;

    ((MainActivity)getActivity()).ButtonBorder(button1, button2, button3, button4);
}

and it doesn't work at all.
Is this the right way of doing it?

You can receive your button variable in following method

//In fragment
lateint var button : Button;

override fun onAttach(context: Context) {
    super.onAttach(context)
    button = context.getButton();

}



// In Activity    
var button = findViewById(R.id.your_button)
getButton(button)

//outside onCreateMethod

fun getButton(var button : Button) : Button
{
  return button
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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