Android java类onClickListener
如何为layout_one中的按钮设置onClick侦听器?我应该把代码放在哪里?当我把它放在onCreateView中时,它给了我一个错误Android java类onClickListener,java,android,sdk,onclicklistener,Java,Android,Sdk,Onclicklistener,如何为layout_one中的按钮设置onClick侦听器?我应该把代码放在哪里?当我把它放在onCreateView中时,它给了我一个错误 public class LayoutOne extends Fragment { Button button; public static Fragment newInstance(Context context) { LayoutOne f = new LayoutOne(); return f;
public class LayoutOne extends Fragment {
Button button;
public static Fragment newInstance(Context context) {
LayoutOne f = new LayoutOne();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
return root;
}
从xml中设置布局属性android:clickable=“true”,
android:focusable=“true”
和android:focusableInTouchMode=“true”
,或从代码中设置布局属性。将onClickListener设置为:
((LinearLayout)findViewById(R.id.layout_one_id)).setClickable(true); ((LinearLayout)findViewById(R.id.layout_one_id)).setOnClickListener(layoutOnClickListener);
private OnClickListener layoutOnClickListener = new OnClickListener() {
public void onClick(View v) {
//Get Click here
}
};
从xml中设置布局属性android:clickable=“true”,
android:focusable=“true”
和android:focusableInTouchMode=“true”
,或从代码中设置布局属性。将onClickListener设置为:
((LinearLayout)findViewById(R.id.layout_one_id)).setClickable(true); ((LinearLayout)findViewById(R.id.layout_one_id)).setOnClickListener(layoutOnClickListener);
private OnClickListener layoutOnClickListener = new OnClickListener() {
public void onClick(View v) {
//Get Click here
}
};
如果你这样做,它不会有任何错误
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater
.inflate(R.layout.layout_one, null);
Button button_one=(Button)root.findViewById(R.id.button_one);// button_one is the id of the button in your xml file
button_one.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
return root;
}
public void onButtonClicked(View v) { /*Stuff*/ }
如果你这样做,它不会有任何错误
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater
.inflate(R.layout.layout_one, null);
Button button_one=(Button)root.findViewById(R.id.button_one);// button_one is the id of the button in your xml file
button_one.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
return root;
}
public void onButtonClicked(View v) { /*Stuff*/ }
不知道按钮的名称,但在
onCreateView()
中,您可以:
button = (Button)root.findViewById(R.id.layout_one_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//stuff goes in here
}
});
您的另一个选择是在xml中执行它,并创建一个使用它的方法。例如,设置按钮
sandroid:onClick=“onButtonClicked”
属性,在代码中,您的方法如下所示
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater
.inflate(R.layout.layout_one, null);
Button button_one=(Button)root.findViewById(R.id.button_one);// button_one is the id of the button in your xml file
button_one.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
return root;
}
public void onButtonClicked(View v) { /*Stuff*/ }
不知道按钮的名称,但在
onCreateView()
中,您可以:
button = (Button)root.findViewById(R.id.layout_one_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//stuff goes in here
}
});
您的另一个选择是在xml中执行它,并创建一个使用它的方法。例如,设置按钮
sandroid:onClick=“onButtonClicked”
属性,在代码中,您的方法如下所示
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater
.inflate(R.layout.layout_one, null);
Button button_one=(Button)root.findViewById(R.id.button_one);// button_one is the id of the button in your xml file
button_one.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
return root;
}
public void onButtonClicked(View v) { /*Stuff*/ }
R.layout.layout\u一个文件是片段的布局文件吗?layout\u一个是布局,但它是显示在ViewPager中的布局,R.layout.layout\u一个文件是片段的布局文件吗?layout\u一个是布局,但它是显示在ViewPager中的布局,