![](/img/trans.png)
[英]Android EditText Disappears after pressing Button in TableLayout
[英]Android TableLayout Add new Row with data from EditText by clicking a button
我在MainActivity
上有一个空的tablelayout
设置,还有另一个名为Add_Item
活动,其中有一些EditText
,用户可以在其中提交信息以添加到tablelayout
。 卡住的部分是如何在用户每次按下保存按钮时将插入到EditText
的数据实际保存到新行中。 是否有教程提供有关如何使用示例代码执行此操作的信息? 我只用不需要的sql查找那些。 下面是到目前为止我关于XML和Add_Item
活动的代码
// Add_Item活动
package com.example.moduleonesimpleapplication;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class Add_Item extends ActionBarActivity {
//variables
EditText TaskNameET;
Spinner SpinType;
Button SaveTodo;
String SpinnerOptions[] = {"OptionOne", "OptionTwo", "OptionThree"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add__item);
//define vars
TaskNameET = (EditText) findViewById(R.id.TaskNameET);
SpinType = (Spinner) findViewById(R.id.spinner1);
SaveTodo = (Button) findViewById(R.id.button1);
//adapter for spinner
ArrayAdapter<String> ard=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, SpinnerOptions);
SpinType.setAdapter(ard);
//onclick todobutton
SaveTodo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//here is the save button, I would like to save the information inputted from the TaskNameET EditText Into the TableLayout
// I have the tablelayout defined as TableRow
//MainActivity.this.TableRow.setTextAlignment();
Add_Item.this.TaskNameET.getText().toString();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add__item, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
//用于表格布局的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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.moduleonesimpleapplication.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="@string/AddItemButton" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
</RelativeLayout>
你的直觉告诉我吗? 现在应该告诉您,您采用的这种方法是死路一条,是一个障碍。 嗯,从技术上讲,您可以逐步破解并向TableLayout
动态添加行,但这仅是... hack。 为什么? 因为,Android SDK附带了更好的选项和功能。
无论哪种方式,您都需要以某种方式保留用户输入的数据。 尽管sqlite不是唯一的选择,但IMO是最具扩展性的解决方案。 这里有一些想法...
TableLayout
使用ListView
代替 现在,继续使用我上面指出的条款搜索一些教程,但由于此处有违本网站的使用条款,因此请不要在此处索取教程。 祝好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.