java 在 android 上为片段实现 OnClickListener
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27728439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
implementing OnClickListener for a fragment on android
提问by Mac Taylor
I have a sliding menu project and inside home layout another layout is called as a fragment :
我有一个滑动菜单项目,在主页布局中,另一个布局称为片段:
this is the HomeFragment.java :
这是 HomeFragment.java :
package info.androidhive.slidingmenu;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HomeFragment extends Fragment {
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
}
I need to import this click listener inside my java class in order to submit a form .
我需要在我的 java 类中导入这个点击监听器才能提交一个表单。
//CheckFal:
btnCheckFalAction = (Button) findViewById(R.id.btnCheckFal);
btnCheckFalAction.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
}
}
But when I add the above code snippet it throws me an error on undefined methods such as findViewById , OnClickListener , onClick
但是当我添加上面的代码片段时,它会在诸如 findViewById 、 OnClickListener 、 onClick 等未定义方法上引发错误
The button is inside this layout fragment_home.xml
该按钮位于此布局 fragment_home.xml 内
<Button
android:id="@+id/btnCheckFal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/relativeLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="@string/CheckFal" />
回答by Pragnesh Ghoda シ
While working with Fragment
, you have to inflate view.
在使用 时Fragment
,您必须扩大视图。
So when you want to use any widget you have to use view object with findViewById()
.
因此,当您想使用任何小部件时,您必须使用带有findViewById()
.
Simply Do like this...
简单地做这样...
public class HomeFragment extends Fragment {
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
btnCheckFalAction = (Button) rootView.findViewById(R.id.btnCheckFal); // you have to use rootview object..
btnCheckFalAction.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
Toast.makeText(getActivity(), "Hello World", Toast.LENGTH_LONG).show();
}
});
return rootView;
}
}
OR try other way..
或尝试其他方式..
public class HomeFragment extends Fragment implements OnClickListener{
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
btnCheckFalAction = (Button) rootView.findViewById(R.id.btnCheckFal); // you have to use rootview object..
btnCheckFalAction.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btnCheckFal :
//your code...
break;
default:
break;
}
}
}
回答by Jigar Joshi
You can use
您可以使用
rootView.findViewById(...)
in your code, because your class doesn't inherit/implement this method
在你的代码中,因为你的类没有继承/实现这个方法
回答by Akira
first...you should findView in rootView such as
首先......你应该在 rootView 中找到View,例如
btnCheckFalAction = (Button) rootView.findViewById(R.id.btnCheckFal);
then you can finish Button's operation
然后就可以完成Button的操作了
回答by ali hussnain
// Inflate the layout for this fragment in on create
RelativeLayout mLinearLayout = (RelativeLayout) inflater.inflate(R.layout.fragment_a,
container, false);
cd = new ConnectionDetector(getActivity());
ImageButton mButton = (ImageButton) mLinearLayout.findViewById(R.id.imageButton1);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// here you set what you want to do when user clicks your button,
// e.g. launch a new activity
Toast.makeText(getActivity(),"Opening Maps",Toast.LENGTH_LONG).show();
}
});
you can call this by view
你可以通过视图调用它