在我的应用程序中,我有同一个片段的多个实例,Activity_Fragment
,显示在LinearLayout中。在每个片段中,有一个activity_text
textview和一个edit_button
。当按下按钮时,它应该改变同一片段中的文本视图的文本。但是,添加片段的多个实例时,任何片段中的任何edit_button
只会更改添加到LinearLayout的第一个片段中的activity_text
textview。这是因为所有片段都是相同的,并且他们的子视图共享相同的ID。每个片段应该彼此独立行事;在片段中按下的任何edit_button
只应该影响该片段。我可以将findViewById()限制在单个片段的范围内吗?
是否有可能将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"/>
这是我与ActivityFragment类MainActivity.java。对于edit_button的onclick方法是在最底层:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public static class ActivityFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_fragment, container,
false);
}
}
public void addActivity(View view) {
ActivityFragment fragment1 = new ActivityFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, fragment1).commit();
}
//This is the editButton method
public void editActivity(View view) {
TextView activityText = (TextView) findViewById(R.id.activity_text);
activityText.setText("success");
}
}
谢谢!我刚刚开始使用Android,而且我刚刚使用Android开发人员指南,因此大部分改变的内容对我来说都是新手,我不太了解其中的一些内容。主要是'@ Override','@ Nullable','root'和'.OnClickListener'。稍后会在Android培训或API指南中介绍吗?你有链接还是知道我可以在这里阅读更多内容? –
'@ Override'和'@ Nullable'可以忽略。它们是帮助您编写不会中断的代码的注释,但它们不会影响写得很好的程序的行为。 'root'只是一个我喜欢在'Fragment.onCreateView()'中使用的变量名(希望你能在片段教程中阅读)。一个'View.OnClickListener'和你的'android:onClick'属性是相同的概念,但是以不同的方式完成。你可以在Button文档中阅读一下它:https://developer.android.com/reference/android/widget/Button.html。最终所有这些都将是第二性质。 –
好的,非常感谢! –