简体   繁体   English

在Android中动态添加EditText

[英]Add EditText dynamically in Android

I add an edittext to view on button click and then I want to be insert values ​​in the SQLite db. 我添加了一个edittext来查看按钮单击,然后我想在SQLite数据库中插入值。 So far I have tried this code to the new EditText, but are created horizontally rather than vertically. 到目前为止,我已经尝试将此代码用于新的EditText,但是是水平创建而不是垂直创建。 Also how do I manage ContentValues ​​to insert the data of the EditText added? 另外如何管理ContentValues以插入添加的EditText数据?

public void Add(View v) {
      EditText et=new EditText(cnt);
      linear.addView(et);
  }    

For adding your edittext vertically to your layout, you can try following codes; 要将编辑文本垂直添加到布局,可以尝试以下代码; I assumed you are using LinearLayout: 我假设您使用的是LinearLayout:

LinearLayout root = (LinearLayout) findViewById(R.id.your_layout_id);
LinearLayout dynamic_component = new LinearLayout(this);
dynamic_component.setOrientation(LinearLayout.VERTICAL);
dynamic_component.addView(et);
root.addView(dynamic_component);

If I understood right, you can try following codes: 如果我理解正确,您可以尝试以下代码:

ContentValues values = new ContentValues();
values.put(YOU_KEY, text);
database.insert(YOUR_TABLE_NAME, null, values);

Set the orientation of your parent view (here linear) to vertical in the xml file where your layout is. 在布局所在的xml文件中将父视图的方向(此处为线性)设置为垂直。

Then you will be able to add EditText views vertically. 然后,您将能够垂直添加EditText视图。

android:orientation="vertical"
<!--default is horizontal. -->

That is whay edittext added are aligning horizontally. 这是添加的edittext水平对齐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM