Android 我可以将findViewById()限制在单个片段的范围内吗?
在我的应用程序中,同一片段的多个实例,Android 我可以将findViewById()限制在单个片段的范围内吗?,android,android-fragments,findviewbyid,android-identifiers,Android,Android Fragments,Findviewbyid,Android Identifiers,在我的应用程序中,同一片段的多个实例,Activity\u fragment,以线性布局显示。在每个片段中,都有一个activity\u texttextview和一个edit\u按钮。在片段中按下按钮时,应更改同一片段中textview的文本。但是,当添加片段的多个实例时,在任何片段中按下的任何edit_按钮都只会更改添加到LinearLayout的第一个片段中的activity_文本textview。这是因为所有片段都是相同的,并且它们的子视图共享相同的id。每个片段都应该相互独立地起作用;
Activity\u fragment
,以线性布局显示。在每个片段中,都有一个activity\u text
textview和一个edit\u按钮
。在片段中按下按钮时,应更改同一片段中textview的文本。但是,当添加片段的多个实例时,在任何片段中按下的任何edit_按钮
都只会更改添加到LinearLayout的第一个片段中的activity_文本
textview。这是因为所有片段都是相同的,并且它们的子视图共享相同的id。每个片段都应该相互独立地起作用;在片段中按下的任何edit_按钮
只会影响该片段
是否可以将findViewById()方法限制在单个片段的范围内,这样一个片段就不会受到另一个片段中事件的影响,或者在创建片段时我必须以某种方式更改片段中每个视图的id
以下是活动片段xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/activity_fragment">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/activity_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Edit Activity"
android:textSize="18sp"
app:layout_constraintBaseline_toBaselineOf="@+id/checkbox"
app:layout_constraintLeft_toRightOf="@+id/checkbox"
app:layout_constraintRight_toLeftOf="@+id/edit_button"/>
<Button
android:id="@+id/edit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="16dp"
android:onClick="editActivity"
android:text="Edit Activity"
app:layout_constraintBaseline_toBaselineOf="@+id/checkbox"
app:layout_constraintRight_toRightOf="parent"/>
您可以使用
view.findViewById(R.id.activity\u text)
代替活动中的方法。您可以使用view.findViewById(R.id.activity\u text)
代替活动中的方法。您的问题可以通过限制findViewById()
扫描的范围来解决。如果要查看特定片段,可能需要调用myFragment.getView().findViewById()
(而不仅仅是从活动中使用findViewById()
)
那你怎么做呢?嗯,你安排事情的方式不容易。由于您使用的是android:onClick
属性来设置单击侦听器,因此必须从活动内部处理事情
所以不要使用android:onClick
相反,在片段的onCreateView()方法中,编写如下内容:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.your_fragment, container, false);
Button button = (Button) root.findViewById(R.id.edit_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView activityText = (TextView) getView().findViewById(R.id.activity_text);
activityText.setText("success");
}
});
return root;
}
您的问题可以通过限制findViewById()
扫描的范围来解决。如果要查看特定片段,可能需要调用myFragment.getView().findViewById()
(而不仅仅是从活动中使用findViewById()
)
那你怎么做呢?嗯,你安排事情的方式不容易。由于您使用的是android:onClick
属性来设置单击侦听器,因此必须从活动内部处理事情
所以不要使用android:onClick
相反,在片段的onCreateView()方法中,编写如下内容:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.your_fragment, container, false);
Button button = (Button) root.findViewById(R.id.edit_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView activityText = (TextView) getView().findViewById(R.id.activity_text);
activityText.setText("success");
}
});
return root;
}
谢谢我刚开始使用安卓系统,我刚刚使用了安卓开发人员指南,所以您更改的大部分内容对我来说都是新的,我对其中一些内容不太了解。主要是@Override
,@Nullable
,root
和.OnClickListener
。Android培训或API指南后面会介绍这一点吗?您是否有链接或知道我在哪里可以阅读更多关于此的内容?@Override
和@Nullable
可以忽略。它们是注释,可以帮助您编写不会中断的代码,但不会影响编写良好的程序的行为root
只是我喜欢在Fragment.onCreateView()
中使用的变量名(希望您可以在片段教程中阅读)。View.OnClickListener
与您的android:onClick
属性的概念相同,但使用的方式不同。您可以在按钮文档中了解一点。最终所有这些都将成为第二天性。谢谢!我刚开始使用安卓系统,我刚刚使用了安卓开发人员指南,所以您更改的大部分内容对我来说都是新的,我对其中一些内容不太了解。主要是@Override
,@Nullable
,root
和.OnClickListener
。Android培训或API指南后面会介绍这一点吗?您是否有链接或知道我在哪里可以阅读更多关于此的内容?@Override
和@Nullable
可以忽略。它们是注释,可以帮助您编写不会中断的代码,但不会影响编写良好的程序的行为root
只是我喜欢在Fragment.onCreateView()
中使用的变量名(希望您可以在片段教程中阅读)。View.OnClickListener
与您的android:onClick
属性的概念相同,但使用的方式不同。您可以在按钮文档中了解一点。最终,所有这些都将成为第二天性。