简体   繁体   English

在android中动态添加文本框

[英]Dynamically adding textbox in android

I am working on an app and need to add a textbox to the View when a button is selected. 我正在开发一个应用程序,需要在选择按钮时向View添加文本框。 How can I do that or add any object dynamically. 我该怎么做或动态添加任何对象。 What class do I use or what method do I need to call? 我应该使用什么课程或者需要拨打什么方法? Thanks. 谢谢。

You just need to call the addView method to the target view. 您只需要将addView方法调用到目标视图即可。 It's a method inherited from ViewGroup , see [here][2]. 这是一个继承自ViewGroup的方法,见[here] [2]。

[2]:http://developer.android.com/reference/android/view/ViewGroup.html#addView(android.view.View, android.view.ViewGroup.LayoutParams) [2]:http://developer.android.com/reference/android/view/ViewGroup.html#addView(android.view.View,android.view.ViewGroup.LayoutParams)

One possibility is to define it in your layout XML, and set: 一种可能性是在布局XML中定义它,并设置:

android:visibility="gone"

Then in your code you can do: 然后在您的代码中,您可以:

TextView myTextBox = (TextView) findViewById(R.id.myTextBoxId);
myTextBox.setVisibility(View.VISIBLE);

and to hide it again: 并再次隐藏它:

myTextBox.setVisibility(View.GONE);

Here is the documentation 这是文档

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

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