为 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;
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 class 中导入此点击侦听器才能提交表单。
//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" />
您可以使用
rootView.findViewById(...)
在你的代码中,因为你的 class 没有 inherit/implement 这个方法
在使用 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;
}
}
}
// 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();
}
});
你可以通过view调用这个
首先...你应该在rootView中找到View如
btnCheckFalAction = (Button) rootView.findViewById(R.id.btnCheckFal);
然后就可以完成Button的操作了
我有一个滑动菜单项目,在主页布局中,另一个布局称为片段:
这是 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 class 中导入此点击侦听器才能提交表单。
//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" />
您可以使用
rootView.findViewById(...)
在你的代码中,因为你的 class 没有 inherit/implement 这个方法
在使用 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;
}
}
}
// 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();
}
});
你可以通过view调用这个
首先...你应该在rootView中找到View如
btnCheckFalAction = (Button) rootView.findViewById(R.id.btnCheckFal);
然后就可以完成Button的操作了