Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从onClick中的片段获取视图_Android_Android Fragments_View_Onclick - Fatal编程技术网

Android 如何从onClick中的片段获取视图

Android 如何从onClick中的片段获取视图,android,android-fragments,view,onclick,Android,Android Fragments,View,Onclick,我有一个带有字符串数组和可绘制数组的活动。我根据数组的长度在onCreate中使用for循环创建片段,并使用字符串数组填充片段中的文本视图 我使片段可以单击,并在片段类中添加了onClick 我想从用户单击的片段中获取文本,但当我单击时,它只从第一个片段中获取文本。如何读取不同片段中的文本视图 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,

我有一个带有字符串数组和可绘制数组的活动。我根据数组的长度在onCreate中使用for循环创建片段,并使用字符串数组填充片段中的文本视图

我使片段可以单击,并在片段类中添加了onClick

我想从用户单击的片段中获取文本,但当我单击时,它只从第一个片段中获取文本。如何读取不同片段中的文本视图

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
        int layoutId = getArguments().getInt("layout");
        View rootView = inflater.inflate(layoutId, container, false);
        TextView fragmentText = (TextView) rootView.findViewById(R.id.fragTextView);
        fragmentText.setText(getArguments().getString("fragText"));
        ImageView fragmentImage = (ImageView) rootView.findViewById(R.id.fragImageView);
        fragmentImage.setImageResource(getArguments().getInt("pic"));
        rootView.setOnClickListener(this);
        return rootView;
    }
        @Override
        public void onClick(View v) {

            String s = ((TextView)v.getRootView().findViewById(R.id.fragTextView)).getText().toString();
            Intent intent = new Intent(v.getContext(), LevelActivity.class);
            intent.putExtra("exerciseId", s);
            startActivity(intent);
        }

谢谢你,而不是v.getRootView。你可以用getView@Blackbelt-非常感谢任何想知道的人:getRootView返回层次结构中最顶层的视图。这就是为什么我总是得到我填写的第一个文本视图。如果你觉得它可以帮助某人,将你的评论作为答案发布,并将其标记为已接受的答案