我有一个包含listView的主要布局,一个EditText和一个按钮。另一个名为row.axml的文件包含TextView和ImageView。我创建了一个AddItem.cs(Fragment)类,它在Main.axml中包含的listView中添加了一个项目。但我不明白如何调用类Additem.cs在listview中添加一个新项目与EditText中输入的文本。
Main.Axml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView android:id="@+id/in"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
android:layout_weight="1"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText android:id="@+id/edit_text_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="bottom"
/>
<Button android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send"
/>
</LinearLayout>
Row.Axml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/singleContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/singleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:background="@drawable/bubble_green"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="@android:color/background_dark" />
</LinearLayout>
MainActivity:
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.Main);
ImageButton btAdd = FindViewById<ImageButton> (Resource.Id.btnAdd);
_ListOfTexts = FindViewById<ListView> (Resource.Id.in);
btAdd.Click += delegate(object sender, EventArgs e) {
string txtAdded = FindViewById<EditText> (Resource.Id.edit_text_out).Text;
//add new row
};
}
Additem.cs
public class AddItem: Fragment
{
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return base.OnCreateView (inflater, container, savedInstanceState);
}
}
答案 0 :(得分:1)
制作一个公共方法,在addItem
中添加项目并从父项活动中调用它。