我想在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" />
这是它的外观:
我一直在使用
android:singleline="true"
试过,我曾尝试不同的布局,但我不真的了解它。我没有用碎片工作过多,所以我可能做了一些根本性的错误。
请发布您的活动的XML布局代码 –
我已经更新了它。 –
其中是 '的XML布局代码? –