我正在创建一个应用程序,其中用户在活动中输入班级名称(例如:Geology 101),并且在其下方,有用户输入作业(家庭作业)及其相应成绩权重(15%)的编辑文本。当用户保存它时,我希望将类的名称存储到另一个活动的列表视图中。因此,当用户查看他看到的日程安排时,Geology 101 并且当他单击该项目时,它会显示各种作业类型,例如家庭作业、测试、测验等,然后您可以单击家庭作业,在这里您将存储输入的各种家庭作业通过另一项活动。
到目前为止,这就是我对各种编辑文本的活动所拥有的。
new_class.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/register_assign_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="@string/example_assign"
android:singleLine="true"
android:inputType="textCapWords"
android:layout_weight="1">
</EditText>
<EditText
android:id="@+id/register_weight_edit"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="@string/example_weight"
android:layout_marginLeft="3dp"
android:gravity="center_horizontal"
android:inputType="number"
android:singleLine="true">
</EditText>
</LinearLayout>
new_class_container.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/gradient_gray">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="3dp"
android:orientation="horizontal">
<TextView
android:id="@+id/register_class_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/write_class_name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"/>
<EditText
android:id="@+id/register_class_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/example_name"
android:inputType="textCapWords"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="3dp">
<TextView
android:id="@+id/register_assign_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/write_assignment_type"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#4169E1"/>
<TextView
android:id="@+id/register_weight_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/write_assign_percent"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#4169E1"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/register_assign_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="@string/example_assign"
android:singleLine="true"
android:inputType="textCapWords"
android:layout_weight="1">
</EditText>
<EditText
android:id="@+id/register_weight_edit"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="@string/example_weight"
android:layout_marginLeft="3dp"
android:gravity="center_horizontal"
android:inputType="number"
android:singleLine="true">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical">
<Button
android:id="@+id/btn_add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:gravity="center_vertical|left"
android:onClick="onAddNewClicked"
android:paddingLeft="5dp"
android:text="@string/more_assign"/>
</LinearLayout>
</LinearLayout>
new_class.java
public class NewClass extends Activity{
private LinearLayout mContainerView;
private Button mAddButton;
private View mExclusiveEmptyView;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.new_class_container);
mContainerView = (LinearLayout) findViewById(R.id.parentView);
mAddButton = (Button) findViewById(R.id.btn_add);
}
@Override
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
}
public void onAddNewClicked(View v){
inflateEditRow(null, null);
v.setVisibility(View.GONE);
}
//Inflates the new_class xml each time I press the Add New Button
private void inflateEditRow(String name, String name1){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.new_class, null);
final EditText register_assign = (EditText) rowView.findViewById(R.id.register_assign_edit);
final EditText et1 = (EditText) rowView.findViewById(R.id.register_weight_edit);
if (name != null && !name.isEmpty() && name1 != null && !name1.isEmpty()) {
register_assign.setText(name);
et1.setText(name1);
} else {
mExclusiveEmptyView = rowView;
}
register_assign.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
if (s.toString().isEmpty()) {
mAddButton.setVisibility(View.GONE);
if (mExclusiveEmptyView != null
&& mExclusiveEmptyView != rowView) {
mContainerView.removeView(mExclusiveEmptyView);
}
mExclusiveEmptyView = rowView;
} else {
if (mExclusiveEmptyView == rowView) {
mExclusiveEmptyView = null;
}
mAddButton.setVisibility(View.VISIBLE);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
mContainerView.addView(rowView, mContainerView.getChildCount()-1);
}
// Add save button to action bar
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.btn_save:
//make it save to the new activity
return true;
default:
return true;
}
}
}
因此,每次我按下“添加更多”按钮时,new_class 布局都会在下方膨胀。正如您在我的代码中看到的那样,保存按钮位于操作栏中,我想单击该按钮并将数据存储在上述方法中。我想知道如何将特定权重分配给特定任务。谢谢。