Java 在eclipse中单击按钮后如何显示项目?

Java 在eclipse中单击按钮后如何显示项目?,java,android,button,Java,Android,Button,在我的应用程序中。。用户可以根据类别输入这些费用,如果他们有多个类别需要输入,则有一个按钮允许用户单击以在同一页面中显示另一组类别(红色矩形框内容) 有什么方法可以做到这一点 多谢各位 我的布局xml文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="mat

在我的应用程序中。。用户可以根据类别输入这些费用,如果他们有多个类别需要输入,则有一个按钮允许用户单击以在同一页面中显示另一组类别(红色矩形框内容)

有什么方法可以做到这一点

多谢各位

我的布局xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f8f9fe"
android:orientation="vertical" >

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

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/recordExpenses"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="15dip"
        android:text="@string/expensesRecord"
        android:textColor="#ff29549f"
        android:textSize="25dip"
        android:textStyle="bold" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/recordDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.59"
        android:text="@string/date"
        android:textSize="25sp" />

    <Spinner
        android:id="@+id/recordDaySpinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.84"
        android:entries="@array/daySpinner" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/recordCategory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/category"
        android:textSize="25sp" />

    <Spinner
        android:id="@+id/recordCategorySpinner"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:entries="@array/categorySpinner"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/recordRM"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.00"
        android:text="@string/rm"
        android:textSize="25sp" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="99dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number" >

        <requestFocus />
    </EditText>
</LinearLayout>

<Button
    android:id="@+id/recordAddCategory"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/addCategory" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/total"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/total"
        android:textSize="25sp" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/balance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/balance"
        android:textSize="25sp" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/backButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/back" />

    <Button
        android:id="@+id/recordSaveButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/save" />

</LinearLayout>


我花了一段时间,但我想我终于得到了你想要的。就我个人而言,我认为这样做的方法是将该部分实现为
列表视图
或动态表布局。我用一个listView做了一个小模型,因为我认为使用它会比较简单

主要活动:

public class MainActivity extends Activity {

    private CustomAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 

        ListView lv = (ListView) findViewById(R.id.listView1);
        adapter = new CustomAdapter(this);
        lv.setAdapter(adapter);

        Button btnAdd_category = (Button) findViewById(R.id.recordAddCategory);
        btnAdd_category.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                adapter.addRow();
            }
        });
    }
listView的自定义适配器

public class CustomAdapter extends BaseAdapter {

    int rowCount = 1;
    private LayoutInflater inflator;

    public CustomAdapter(Context context) {
        super();
        inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

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

    @Override
    public View getView(int postion, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflator.inflate(R.layout.row, parent, false);            
        }
        return convertView;
    }

    public void addRow() {
        rowCount++;
        notifyDataSetChanged();
    }
}
如您所见,整个机制取决于
getCount()
方法。当您添加或减少它返回的数字时,它会告诉适配器您需要多少行。为了修改它,我们制作了一个名为
addRow()
的方法。其余的应该不言自明。这里被膨胀的
xml将是您希望在按下按钮时重复的红色框线布局。我建议将整个页面放在
滚动视图中,因为如果额外的行使视图超出范围,您将需要它



此外,这与您的问题无关,但我认为您需要重新考虑xml布局。通常,使用比不使用更多的视图组(
LinearLayout
等)会大大降低效率。也许,如果您愿意,可以尝试使用
RelativeLayout
进行旋转,或者找到一种方法将所有内容打包到一个
LinearLayout
,因为它设计用于堆叠多行视图,与您所拥有的视图非常相似,没有额外的容器

请发布您的代码,以便我从中获得答案,以及这个问题与eclipse的关系如何?代码已添加。。我正在使用eclipse开发我的应用程序。。