2014-12-08 2 views
0

내가 XML에 글고에서 텍스트를 얻기 위해 노력하고 있어요 내 글고에서 텍스트를 가져 오는 방법을 나는이 :`는 조각이 아니라 활동

이이 내 내 XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@color/background" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/name" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="5dp" 
       android:layout_marginLeft="30dp" 
       android:layout_marginTop="15dp" 
       android:text="Name" 
       android:textColor="@color/accent_red" 
       android:textSize="25sp" /> 

      <EditText 
       android:id="@+id/editContactName" 
       android:layout_width="match_parent" 
       android:layout_height="50dp" 
       android:background="@drawable/border" 
       android:ems="10" 
       android:hint="Name" 
       android:imeOptions="actionNext" 
       android:inputType="textPersonName" 
       android:paddingLeft="30dp" 
       android:paddingRight="30dp" 
       android:textSize="30sp" /> 
     </LinearLayout> 
    </ScrollView> 

</LinearLayout> 

입니다 내가 활동을 확장하고 있지 않다되지 callfindViewById(R.id.editContactName) 수 있기 때문에 것처럼 클래스 분류 SHConfigureContactFragment는

public class SHConfigureContactFragment extends Fragment{ 

    private EditText name; 
    private EditText description; 
    private EditText primaryNumber; 
    private EditText secondaryNumber; 
    private EditText email; 
    private EditText skype; 
    private Byte[] photo; 

    private Boolean isDualPane; 
    private FragmentManager fragmentManager; 
    private SHContactMenuFragment menuFragment; 
    private SHConfigureContactFragment contactFragment; 

    private DatabaseControllerLibrary databaseController; 
    private Contact contact; 

    public int selectedIndex; 

    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     if(savedInstanceState != null) { 
      this.selectedIndex = savedInstanceState.getInt("SELECTED_INDEX"); 
      this.contact = (Contact) savedInstanceState.getSerializable("CONTACT"); 
     } 
    } 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

    } 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
     super.onCreateView(inflater, container, savedInstanceState); 
     View rootView = inflater.inflate(R.layout.sh_fragment_contact_edit, container, false); 
     return rootView; 
    } 

    private void setupTextFieldsByContact(Contact contact) { 
     name = (EditText) findViewById(R.id.editContactName); 
//  description = (EditText) findViewById(R.id.editContactDescription); 
//  primaryNumber = (EditText) findViewById(R.id.editContactPrimaryNumber); 
//  secondaryNumber = (EditText) findViewById(R.id.editContactSecondaryNumber); 
//  email = (EditText) findViewById(R.id.editContactEmail); 
//  skype = (EditText) findViewById(R.id.editContactSkype); 

     if (contact != null) { 
      name.setText(contact.getName()); 
      description.setText(contact.getDescription()); 
      primaryNumber.setText(contact.getPrimaryNumber()); 
      secondaryNumber.setText(contact.getSecondaryNumber()); 
      email.setText(contact.getEmail()); 
      skype.setText(contact.getSkype()); 
     } 
    } 


} 

보인다. 나는 여기에 다양한 게시물을 보았지만 해결책을 찾지 못했습니다. getText() 메서드를 사용하는 방법에 대해 생각했지만 특정 EditText 요소를 가져 오지 못합니다. 편집 가능한 입력란이 여러 개있어 특정 ID로 가져와야하는 이유가

답변

1

, 이렇게 :

name = (EditText) rootView.findViewById(R.id.editContactName); 

그것을 희망 문제 해결에 도움이 될 것입니다.

+0

정확히'rootView'는 무엇이며 어떻게 접근합니까? – jfmg

3

onCreateView(...)에서 부 풀린보기에 액세스하려면 getView()으로 전화하십시오. 당신은 onCreateView(...) 후 언제든지 다음 코드를 사용할 수 있습니다

다음
getView().findViewById(R.id.editContactName) 

나는 조각 안에 어떻게 할 것인지의 다음 onCreateView 방법에서

EditText mName, mDescription, mPrimaryNumber, mSecondaryNumber, mEmail, mSkype; 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    // no need for super here, the default fragment has no layout 
    // inflate the layout in this method 
    View rootView = inflater.inflate(R.layout.sh_fragment_contact_edit, container, false); 
    return rootView; 
} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    // save views as variables in this method 
    // "view" is the one returned from onCreateView 
    mName = (EditText) view.findViewById(R.id.editContactName); 
    mDdescription = (EditText) view.findViewById(R.id.editContactDescription); 
    mPrimaryNumber = (EditText) view.findViewById(R.id.editContactPrimaryNumber); 
    mSecondaryNumber = (EditText) view.findViewById(R.id.editContactSecondaryNumber); 
    emEail = (EditText) view.findViewById(R.id.editContactEmail); 
    mSkype = (EditText) view.findViewById(R.id.editContactSkype); 
} 

private void setupTextFieldsByContact(Contact contact) { 
    // call this method anytime the contact changes without having to find views again 
    if (contact != null) { 
     mName.setText(contact.getName()); 
     mDescription.setText(contact.getDescription()); 
     mPrimaryNumber.setText(contact.getPrimaryNumber()); 
     mSecondaryNumber.setText(contact.getSecondaryNumber()); 
     mEmail.setText(contact.getEmail()); 
     mSkype.setText(contact.getSkype()); 
    } 
}