在android上为片段实现OnClickListener [英] implementing OnClickListener for a fragment on android
问题描述
我有一个滑动菜单项目,在内部布局中,另一个布局被称为片段:
这是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;
公共类HomeFragment extends Fragment {
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState){
查看rootView = inflater.inflate(R.layout.fragment_home,container,false);
返回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();
}
}
<但是,当我添加上面的代码片段时,它会在未定义的方法上抛出错误,例如findViewById,OnClickListener,onClick
按钮位于此布局fragment_home.xml
<按钮
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/>
使用 Fragment
时,你必须充气查看。
因此,当您想要使用任何小部件时,您必须使用 findViewById()
。 / p>
简单地这样做......
公共类HomeFragment扩展Fragment {
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState){
查看rootView = inflater.inflate(R.layout.fragment_home,container,false);
btnCheckFalAction =(Button)rootView.findViewById(R.id.btnCheckFal); //你必须使用rootview对象..
btnCheckFalAction.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
Toast.makeText(getActivity(),Hello World,Toast.LENGTH_LONG)。show();
}
});
返回rootView;
}
}
或尝试其他方式..
public class HomeFragment extends Fragment实现OnClickListener {
public HomeFragment(){}
@覆盖
public View onCreateView(LayoutInflater inflater,ViewGroup容器,
Bundle savedInstanceState){
查看rootView = inflater.inflate(R.layout.fragment_home,container,false);
btnCheckFalAction =(Button)rootView.findViewById(R.id.btnCheckFal); //你必须使用rootview对象..
btnCheckFalAction.setOnClickListener(this);
返回rootView;
}
@Override
public void onClick(查看v){
// TODO自动生成方法stub
switch(v.getId()){
case R.id.btnCheckFal:
//你的代码...
break;
默认值:
break;
}
}
}
I have a sliding menu project and inside home layout another layout is called as a fragment :
this is the 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 .
//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
The button is inside this layout 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" />
While working with Fragment
, you have to inflate view.
So when you want to use any widget you have to use view object with 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;
}
}
}
这篇关于在android上为片段实现OnClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!