gpt4 book ai didi

java - eclipse中点击按钮后如何显示项目?

转载 作者:行者123 更新时间:2023-12-02 07:25:11 25 4
gpt4 key购买 nike

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

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

非常感谢。

screenshot of expenses page

我的布局 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 做了一个小模型,因为我认为使用它会更方便。

主要 Activity :

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() 的方法。其余的应该是不言自明的。这里膨胀的 row xml 只是您想要在按下按钮时重复的红色框内的 LinearLayout。我建议将整个页面放入 ScrollView 中,因为如果额外的行导致 View 超出范围,您将需要它。

<小时/>

此外,这与你的问题无关,但我认为你重新考虑你的 xml 布局。一般来说,使用比您可以管理的更多的 viewGroups(LinearLayout 等),效率会低得多。也许,如果您愿意的话,可以尝试一下 RelativeLayout ,或者找到一种方法将所有内容都装在一个 LinearLayout 中,因为它是为堆叠多行 View 而设计的就像你拥有的一样,没有额外的容器。

关于java - eclipse中点击按钮后如何显示项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13658682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com