我已经在按钮上实施了onclicklistener来打开图库,当我在应用程序中单击该按钮时没有反应
代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_statusfragment,container,false);
imageView = view.findViewById(R.id.imageView);
button = (Button)view.findViewById(R.id.button2);
textView = view.findViewById(R.id.textView);
view.findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),"Upload screenshot",Toast.LENGTH_LONG).show();
if(ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
requestPermissions(
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
2000);
}else {
Toast.makeText(getActivity(), "Choose Screenshot", Toast.LENGTH_LONG).show();
imageView.setVisibility(View.VISIBLE);
button.setVisibility(View.VISIBLE);
textView.setVisibility(View.VISIBLE);
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(intent,1000);
}
}
}
});
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_statusfragment, container, false);
}
它也不显示Toast消息
答案 0 :(得分:2)
代替return inflater.inflate(R.layout.fragment_statusfragment, container, false);
做return view;
答案 1 :(得分:0)
您已经在开始时初始化inflater.inflate(R.layout.fragment_statusfragment, container, false);
并将其分配给View view
,所以在末尾仅return view;
。
答案 2 :(得分:0)
非常首先尝试用onViewCreated()
的{{1}}方法完成所有UI。
在这里,您没有最后一次返回上面初始化的Fragment
,而是使用view
变量来实现点击侦听器。
button