繁体   English   中英

如何从片段而不是活动中的EditText获取文本

[英]how to get text from EditText within a Fragment and not activity

我正在尝试从具有XML的EditText中获取文本:

这是我的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>

这是我的分类为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());
        }
    }


}

好像我不能callfindViewById(R.id.editContactName)因为我没有扩展活动。 我在这里查看过各种帖子,但没有找到解决方案。 我考虑过使用getText()方法,但是那不能让我得到特定的EditText元素。 我希望有许多可编辑的字段,这就是为什么我需要获得特定ID的原因

您可以通过调用getView()来访问onCreateView(...)膨胀的视图。 您可以在onCreateView(...)之后随时使用以下代码:

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

这是我要在片段中执行的操作:

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());
    }
}

onCreateView方法中,执行以下操作:

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

希望它能帮助您解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM