我有一个滑动菜单项目,在内部布局中,另一个布局称为片段:
这是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;
}
}
我需要在我的java类中导入这个click侦听器才能提交表单。
//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();
}
}
但是当我添加上面的代码片段时,它会在未定义的方法上引发错误,例如findViewById,OnClickListener,onClick
该按钮位于此布局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" />
答案 0 :(得分:17)
使用Fragment
时,您必须对视图进行充气。
因此,当您想要使用任何小部件时,您必须使用findViewById()
的视图对象。
简单地这样做......
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;
}
}
或尝试其他方式..
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;
}
}
}
答案 1 :(得分:3)
您可以使用
rootView.findViewById(...)
在您的代码中,因为您的类没有继承/实现此方法
答案 2 :(得分:2)
首先......您应该在rootView中找到View,例如
btnCheckFalAction = (Button) rootView.findViewById(R.id.btnCheckFal);
然后你可以完成Button的操作
答案 3 :(得分:0)
// 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();
}
});
你可以通过视图
来调用它