我想在片段布局中制作一个锁定屏幕按钮 我搜索了一些帮助并发现了相同的内容,但使用了一个Activity。
代码在这里: http://karanbalkar.com/2014/01/tutorial-71-implement-lock-screen-in-android/
我不知道如何改变它以片段形式工作?
请帮忙
我的代码:
public class Tab1fragment extends Fragment {
/**
* @param args
*/
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.tab1_layout, container, false);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
答案 0 :(得分:1)
您必须详细了解Fragment
:http://developer.android.com/reference/android/app/Fragment.html
首先,您必须在片段中实现View.OnClickListener
,然后在onCreateView
内创建一个视图,如:
View rootView = inflater.inflate(R.layout. tab1_layout, container, false);
所以你的片段变成了:
public class Tab1fragment extends Fragment implements View.OnClickListener {
.
.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1_layout, container, false);
mDevicePolicyManager = ...
mComponentName = ...
Button btnEnableAdmin = (Button) rootView.findViewById(R.id.btnEnable);
.
.
btnEnableAdmin.setOnClickListener(this);
.
.
return rootView;
}
public void onClick(View v) {
switch (v.getId()) {
...
}
}
}
注意:我在设备上测试了这段代码