0

在我的应用程序中,我有同一个片段的多个实例,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"); 
    } 
} 

回答

1

您的问题可以通过限制findViewById()扫描的范围来解决。如果您想查看特定片段,您可能需要拨打myFragment.getView().findViewById()(而不是仅使用您的活动中的findViewById())。

那么你怎么能做到这一点?那么,你设置事物的方式并不容易。由于您使用属性来设置您的点击侦听器,因此您必须从活动内部处理事情。因此不要使用。

相反,你的碎片的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; 
} 
+0

谢谢!我刚刚开始使用Android,而且我刚刚使用Android开发人员指南,因此大部分改变的内容对我来说都是新手,我不太了解其中的一些内容。主要是'@ Override','@ Nullable','root'和'.OnClickListener'。稍后会在Android培训或API指南中介绍吗?你有链接还是知道我可以在这里阅读更多内容? –

+0

'@ Override'和'@ Nullable'可以忽略。它们是帮助您编写不会中断的代码的注释,但它们不会影响写得很好的程序的行为。 'root'只是一个我喜欢在'Fragment.onCreateView()'中使用的变量名(希望你能在片段教程中阅读)。一个'View.OnClickListener'和你的'android:onClick'属性是相同的概念,但是以不同的方式完成。你可以在Button文档中阅读一下它:https://developer.android.com/reference/android/widget/Button.html。最终所有这些都将是第二性质。 –

+0

好的,非常感谢! –

0

可以使用view.findViewById(R.id.activity_text)而不是从活动的方法。

相关问题