如何点击Eclipse中的按钮后,显示的项目? [英] How to display item after clicked the button in eclipse?

查看:150
本文介绍了如何点击Eclipse中的按钮后,显示的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序。用户可以在那里的费用的基础上的分类码,如果他们有超过1类别键,还有有一个按钮,允许用户点击,显示另一组的类别(红色长方形框的内容)在同一个页面。

In my apps.. the user can key in there expenses based on category, if they have more than 1 category to key in, there have a button to allow user to click on to display another set of the category (red rectangle box content) in the same page.

任何方法可以做到这一点?

Any method can do this?

非常感谢你。

我的布局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 或动态表格布局。我做了一个小模拟了一个ListView的,因为我会觉得这是一起工作少一点麻烦。

it took me a while but i think i finally got what you wanted. Personally I think the way you'd do this is by implementing that section as a listView or a dynamic table layout. I did a small mock-up with a listView as i'd think it'd be less cumbersome to work with.

主要活动:

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自定义适配器

The custom adapter for the 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被夸大,简直是将您要在$ P $反复红色盒装的LinearLayout pssing按钮。我建议你​​把整个页面在滚动型,因为你会需要它,如果其他行做意见去出界。

As you can see, the entire mechanism is hinged on the getCount() method. when you add or decrease the number it returns, it tells the adapter how many rows you want inflated. To modify it we made a method called addRow(). The rest should be self-explanatory. The row xml here being inflated would simply be that red boxed linearLayout that you want repeated upon pressing the button. I'd recommend putting the whole page in a ScrollView because you're gonna need it if the additional rows make views go out of bounds.

此外,这无关你的问题,但我想你重新考虑你的XML布局。一般使用较多的viewGroups(的LinearLayout 等)相比,可以不用管理是效率要低得多。也许,如果它会讨好你,尝试服用 RelativeLayout的兜风,或找到一种方法来框在一个一切的LinearLayout ,因为它被设计用于堆叠的意见多行就像你有没有额外的容器。

Moreover, this has nothing to do with your question, but i think you reconsider your xml layout. in general using more viewGroups (LinearLayout, etc.) than you can manage without is much less efficient. Perhaps, if it'd please you, try taking RelativeLayout for a spin or find a way to box everything in one LinearLayout, as it was designed for stacking multiple rows of views much like you have, without extra containers.

这篇关于如何点击Eclipse中的按钮后,显示的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆