Java 在android上实现片段的OnClickListener
我有一个滑动菜单项目,在主布局中,另一个布局称为片段: 这是HomeFragment.java:Java 在android上实现片段的OnClickListener,java,android,android-layout,Java,Android,Android Layout,我有一个滑动菜单项目,在主布局中,另一个布局称为片段: 这是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
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类中导入这个点击监听器
//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();
}
}
但是当我添加上面的代码片段时,它会在未定义的方法上抛出一个错误,例如findviewbyd、OnClickListener、onClick
按钮位于此布局片段_home.xml内
您可以使用
rootView.findViewById(...)
在您的代码中,由于您的类在处理
片段时不继承/实现此方法,因此必须膨胀视图
因此,当您想要使用任何小部件时,必须将view object与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;
}
}
}
您可以首先通过视图调用它,您应该在rootView中找到视图,例如
btnCheckFalAction = (Button) rootView.findViewById(R.id.btnCheckFal);
然后你就可以完成按钮的操作了谢谢,但是其他方法,比如OnClickListener和onClick,它仍然会给我带来未定义的错误,因为使用-checkout API文档“android.View无法解析为类型”这是使用android.view.view后出现的错误,很抱歉出现了输入错误,但此文件中未定义OnClickListener类,并显示创建类确保导入了正确的类文件,即import android.view.view.OnClickListener代码>就是这样,那么这个呢:“Toast类型中的方法makeText(Context,CharSequence,int)不适用于参数(new View.OnClickListener(){},String,int)”我不清楚该方法:我是这样使用的:Toast.makeText(this.getBaseContext(),“Hello World”,Toast.LENGTH\u LONG)。show(); 仍然是错误而不是这个.getBaseContext()尝试使用简单的getActivity(),即Toast.makeText(getActivity(),“Hello World”,Toast.LENGTH_LONG).show();
// 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();
}
});
btnCheckFalAction = (Button) rootView.findViewById(R.id.btnCheckFal);