使用Fragments处理android中的EditText

时间:2015-11-21 15:48:01

标签: android android-fragments android-edittext

我正在尝试在Android上开发一个应用程序,我遇到了一些EditText和一个新活动的问题。

我有一个我想要三个EditText项目的活动。在每个EditText中,我从JSONObject读取一个字符串,我将其放入EditText中。活动的目的是能够编辑EditText项目中的内容。

对我来说奇怪的是,当我在布局文件中放置三个EditText项时,打开活动时有六行。我不明白为什么它会像那样。

这是Activity类及其片段和xml文件。

EditDate.java:

public class EditDate extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_edit_date);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        EditDateFragment editDateFragment = new EditDateFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.edit_date_container, editDateFragment).commit();
        }

}

EditDateFragment.java:

public class EditDateFragment extends Fragment {
    private JSONObject outerObject;

    public EditDateFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Intent intent = getActivity().getIntent();
        int POSITION = intent.getIntExtra("POSITION", -1);
        EditText editDay = (EditText) this.getActivity().findViewById(R.id.edit_day);
        EditText editMonth = (EditText) this.getActivity().findViewById(R.id.edit_month);
        EditText editYear = (EditText) this.getActivity().findViewById(R.id.edit_year);
        if (POSITION >= 0) {
            try {
                String day = getDay(POSITION);
                String month = getMonth(POSITION);
                String year = getYear(POSITION);
                editDay.setText(day);
                editMonth.setText(month);
                editYear.setText(year);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("POSITION IS NEGATIVE");
        }
        return inflater.inflate(R.layout.fragment_edit_date, container, false);
    }
    @Override
    public void onCreate(Bundle bundleSavedInstanceState){
        super.onCreate(bundleSavedInstanceState);
        outerObject = MainActivity.usrObject;

    }

    private String getDay(int POSITION) throws JSONException{
        JSONArray list = outerObject.getJSONArray(MainActivity.LIST);
        JSONObject dayObject = list.getJSONObject(POSITION);
        JSONObject date = dayObject.getJSONObject(MainActivity.LIST_DATE);
        String day = date.getString(MainActivity.LIST_DAY);
        return day;
    }
    private String getMonth(int POSITION) throws JSONException{
        JSONArray list = outerObject.getJSONArray(MainActivity.LIST);
        JSONObject dayObject = list.getJSONObject(POSITION);
        JSONObject date = dayObject.getJSONObject(MainActivity.LIST_DATE);
        String month = date.getString(MainActivity.LIST_MONTH);
        return month;
    }
    private String getYear(int POSITION) throws JSONException{
        JSONArray list = outerObject.getJSONArray(MainActivity.LIST);
        JSONObject dayObject = list.getJSONObject(POSITION);
        JSONObject date = dayObject.getJSONObject(MainActivity.LIST_DATE);
        String year = date.getString(MainActivity.LIST_YEAR);
        return year;
    }
}

fragment_edit_date.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.erikbylow.shootingapplication.EditDateFragment"
    tools:showIn="@layout/activity_edit_date"
    android:orientation="vertical"
    android:id="@+id/edit_date_container"
    >
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edit_day"/>
   <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edit_month"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edit_year"/>
</LinearLayout>

acticity_edit_date.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.erikbylow.shootingapplication.EditDate">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_edit_date" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

和content_edit_date.xml

<fragment 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:id="@+id/fragment"
    android:name="com.example.erikbylow.shootingapplication.EditDateFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:layout="@layout/fragment_edit_date" />

它的外观如下:

Three EditText items gives 6 lines

我尝试过使用

android:singleline="true" 

我尝试过不同的布局,但我真的不明白。我没有那么多碎片工作,所以我可能做了一些根本错误的事情。

2 个答案:

答案 0 :(得分:2)

这种情况正在发生,因为您已将R.layout.fragment_edit_date设置为布局视图,并且您添加了与视图中的活动视图相同的视图的相同布局

        getSupportFragmentManager().beginTransaction().add(R.id.edit_date_container, editDateFragment).commit();

acticity_edit_date.xml:更改为以下

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.erikbylow.shootingapplication.EditDate">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<FrameLayout
 android:id="@+id/fragment_container"
 android:layout_width="match_parent"
 android:layout_height="match_parent" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

和活动代码如下

public class EditDate extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.acticity_edit_date.xml);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    EditDateFragment editDateFragment = new EditDateFragment();
    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, editDateFragment).commit();
    } 

}

答案 1 :(得分:0)

getSupportFragmentManager()
    .beginTransaction()
    .add(R.id.edit_date_container, editDateFragment)
    .commit();

add(R.id.edit_date_container, editDateFragment)的第一个参数应该是您要添加片段的视图的ID,而不是您要添加的布局ID。

首先,在活动布局中删除此行:

<include layout="@layout/content_edit_date" />

并提出类似的内容:

<FrameLayout
   android:id="@+id/my_container"
   android:layout_width="match_parent"
   android:layout_height="match_parent" />

然后,在你的Activity中写下这段代码:

getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.my_container, editDateFragment)
        .commit();