在 Android 中以编程方式生成编辑文本 [英] Generating Edit Text Programmatically in Android

查看:20
本文介绍了在 Android 中以编程方式生成编辑文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发联系人应用程序,它添加了电子邮件地址、电话号码.我必须在代码本身中动态创建编辑文本.我不知道如何以及在哪里实现这个逻辑,建议任何帮助将不胜感激.

I'm developing Contact Application, which adds Email address, phone number. I have to create edit text dynamically in code itself. I don't know how and where to implement this logic, suggest any help would be grateful.

推荐答案

你可以这样创建:

EditText myEditText = new EditText(context); // Pass it an Activity or Context
myEditText.setLayoutParams(new LayoutParams(..., ...)); // Pass two args; must be LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, or an integer pixel value.
myLayout.addView(myEditText);

这可以在 UI 线程的任何地方实现;一个点击监听器,一个 onCreate 方法,以及介于两者之间的一切.

This can be implemented anywhere on the UI thread; a click listener, an onCreate method, and everything in between.

在这个问题中有一个更通用的例子,以及这些流程的详细介绍在此博客中一个>.

There is a more generic example in this question, and a good rundown of these processes in this blog.

这篇关于在 Android 中以编程方式生成编辑文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆