Android 在回收片段中为按钮放置onClickListener的位置
这里是android/java的新手。我正试图创建一个应用程序,用回收的碎片控制互联网上的电源开关。我创建了一个类来定义每个远程开关的值。在创建片段的新实例时,我将它们作为带有setArguments的包传递进来。 我假设,因为我正在为多个远程交换机回收组件,所以我需要一个单独的片段活动类来处理这些脏活。我需要做一些事情,比如检测一个按钮的点击以发送一个json调用来触发远程开关的切换,然后处理一个回调(最终)以在远程响应它实际切换了中继时实际更改指示器。 我假设这些都是在Activity类中发生的事情,但首先我不确定如何访问片段中的属性(包括远程开关的定义以及我从何处拉出布局项的句柄)为了操纵片段显示并将onClickListener分配给切换按钮等 我需要做的第一件事是知道应该在哪里定义按钮的onClickListener。我知道我可以用另一个findviewbyd检索按钮,但当它们已经在片段中定义时,这似乎有点愚蠢。我只是不知道如何从活动本身返回片段来访问其中的属性和方法。我也不确定点击监听器应该定义在哪里(传统上) 这是我的片段代码: res/layout/fragment_piswitch.xml PIS断裂活动Android 在回收片段中为按钮放置onClickListener的位置,android,android-fragments,button,onclicklistener,android-fragmentactivity,Android,Android Fragments,Button,Onclicklistener,Android Fragmentactivity,这里是android/java的新手。我正试图创建一个应用程序,用回收的碎片控制互联网上的电源开关。我创建了一个类来定义每个远程开关的值。在创建片段的新实例时,我将它们作为带有setArguments的包传递进来。 我假设,因为我正在为多个远程交换机回收组件,所以我需要一个单独的片段活动类来处理这些脏活。我需要做一些事情,比如检测一个按钮的点击以发送一个json调用来触发远程开关的切换,然后处理一个回调(最终)以在远程响应它实际切换了中继时实际更改指示器。 我假设这些都是在Activity类中发
public class PiSwitchFragmentActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//////////////////////////////////////////////////////////////
// not sure how to access the associated fragment from here //
//////////////////////////////////////////////////////////////
// where should onClickListener for piToggle1 button be defined? //
};
}
好的,我在FragmentActivity超类中发现了一个片段,它似乎在onCreate之前运行,所以我假设我可以根据提交的片段在那里设置一个全局。那么onCreate是放置按钮侦听器的好地方吗?您也可以放入onCreate。结果发现,片段不是我想要的,我切换到ListView,并在客户适配器中添加了onClick代码
public class PiSwitchFragment extends Fragment {
PiSwitchFragmentActivity listener;
private PiSwitch fragSwitch = new PiSwitch();
public Button fragToggle;
public Switch fragStatus;
public TextView fragInfo;
public PiSwitchFragment() {
// Required empty public constructor
}
// This event fires 1st, before creation of fragment or any views
// The onAttach method is called when the Fragment instance is associated with an Activity.
// This does not mean the Activity is fully initialized.
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Activity){
this.listener = (PiSwitchFragmentActivity) context;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// check for arguments to initialize the PiSwitch
Bundle args = getArguments();
if(args != null) {
this.fragSwitch.setPiName(args.getString("name","(Unknown)"));
this.fragSwitch.setPiId(args.getCharArray("id"));
this.fragSwitch.setSwitchPin(args.getInt("pin",0));
this.fragSwitch.setSwitchState(args.getBoolean("state", false));
this.fragSwitch.setPiAddress(args.getString("addr",""));
}
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_piswitch, container, false);
}
// This event is triggered soon after onCreateView().
// Any view setup should occur here. E.g., view lookups and attaching view listeners.
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// Setup any handles to view objects here
this.fragToggle = (Button)view.findViewById(R.id.piToggle1);
this.fragStatus = (Switch)view.findViewById(R.id.piStatus1);
this.fragInfo = (TextView)view.findViewById(R.id.piInfo1);
this.updateInfo(this.fragSwitch.getPiName()); // update the display name
this.updateStatus(this.fragSwitch.getSwitchState()); // set the current displayed switch state
}
public void updateStatus(Boolean status) {
this.fragStatus.setChecked(status);
}
public void updateInfo(String inf) {
this.fragInfo.setText(inf);
}
}
public class PiSwitchFragmentActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//////////////////////////////////////////////////////////////
// not sure how to access the associated fragment from here //
//////////////////////////////////////////////////////////////
// where should onClickListener for piToggle1 button be defined? //
};
}