我已经在home_fragment.xml
中添加了一个浮动的共享按钮。但是我不知道从哪里开始给共享按钮添加功能。请帮帮忙。
这是片段java文件的代码,我试过写代码但失败了,如果有人能帮上忙,我会很高兴的。
public class HomeFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home, null);
}
}
发布于 2018-08-28 23:24:57
public class HomeFragment extends Fragment {
Button share_bt;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView= inflater.inflate(fragment_home, container, false);
FloatingActionButton share_bt= rootView.findViewById(R.id.share_bt);
share_bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(Intent.ACTION_SEND);
myIntent.setType("text/plain");
String shareBody = "hu" ;
String shareSub = "mk";
myIntent.putExtra(Intent.EXTRA_SUBJECT,shareBody);
myIntent.putExtra(Intent.EXTRA_TEXT,shareSub);
startActivity(Intent.createChooser(myIntent, "Share Using"));
}
});
return rootView;
}
}
发布于 2018-08-28 22:34:56
您可以使用View.OnClickListener在单击按钮时调用回调方法。有关详细信息,请参阅https://developer.android.com/reference/android/view/View.OnClickListener。
就像@kAliert已经回答的那样,你必须获得View firstand的一个实例,有很多类似的问题和答案可能会有所帮助。
对任何样式或语法错误提前道歉
以下是如何在片段中执行此操作的示例:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View homeFragmentView= inflater.inflate(R.layout.fragment_home, container, false);
FloatingActionButton animationDetailShare= homeFragmentView.findViewById(R.id.animation_detail_share);
btnCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//The logic for the button
}
});
return homeFragmentView;
}
发布于 2018-08-28 22:21:33
获取视图的一个实例并找到该按钮。这并不难,我猜它已经回答了很多次了。
https://stackoverflow.com/questions/52059521
复制相似问题