0

我正在创建一个应用程序,其中用户在活动中输入班级名称(例如: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_container 的样子

因此,每次我按下“添加更多”按钮时,new_class 布局都会在下方膨胀。正如您在我的代码中看到的那样,保存按钮位于操作栏中,我想单击该按钮并将数据存储在上述方法中。我想知道如何将特定权重分配给特定任务。谢谢。

4

1 回答 1

0

那么,您是否需要这样做(将新视图膨胀到容器中)?或者你只是简单地使用 ListView,只需添加新项目,刷新视图,它就在那里。你可以看看这个例子:

主要活动:

public class MainActivity extends Activity {

    private Button add;
    private MyAdapter adapter;
    private ArrayList<String> data;
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        add = (Button) findViewById(R.id.button1);
        lv = (ListView) findViewById(R.id.listView1);
        data = new ArrayList<String>();
        adapter = new MyAdapter(data);
        lv.setAdapter(adapter);
        add.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                data.add("Add new item and its properties, class name, assignment type, grade...");
                // Here you can store into your data storage(database,etc) to display later
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

和适配器:

class MyAdapter extends BaseAdapter{

        private ArrayList<String> data;

        public MyAdapter(ArrayList<String> data) {
            this.data = data;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return data.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return data.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {
            View view = arg1;
            if(view == null){
                view = getLayoutInflater().inflate(R.layout.item, null);
            }
            return view;
        }
    }

和 XML 容器:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
         >
    </ListView>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Add more" />

</RelativeLayout>

物品:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name of class:"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:text="Asignment Type"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignLeft="@+id/editText1"
        android:text="Grade Weight(%)"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:ems="10" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText2"
        android:layout_alignBottom="@+id/editText2"
        android:layout_alignRight="@+id/textView3"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10" />

</RelativeLayout>

希望这可以帮助。

于 2013-08-10T04:27:27.507 回答