2015-11-21 88 views
0

我想在android上开发一个应用程序,并且遇到了一些EditText和一项新活动的问题。在android中使用碎片处理EditText

我有一个活动,我想要三个EditText项目。在每个EditText中,我从JTextObject中读取一个字符串,并将其放入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" 

试过,我曾尝试不同的布局,但我不真的了解它。我没有用碎片工作过多,所以我可能做了一些根本性的错误。

+0

请发布您的活动的XML布局代码 –

+0

我已经更新了它。 –

+0

其中是'的XML布局代码? –

回答

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

}

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

add(R.id.edit_date_container, editDateFragment)第一个参数应视标识要添加的片段,您要添加的布局不是一个编号。

首先,删除这一行活动布局:

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

,并把这样的事情:

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

然后,在你的活动写这样的代码:

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

我已尝试了,我得到一个异常: java.lang.IllegalArgumentException异常:未找到ID 0x7f0c006b视图(com.example.erikbylow.shootingapplication:ID/my_container)的片段EditDateFragment –

+0

确保您拥有的FrameLayout ID为:my_container在你的活动布局 – RediOne1

+0

是的,我有。 –