Dev*_*raj 10 dynamically-generated android-edittext
我正在开发一个应用程序,我必须EditText
动态创建多个
和Spinner.所以我开始寻找解决方案,因为我没有权限
在XML文件中使用Invisible属性.我搜索了很多,只在stackoverflow上得到了一些例子.我关注它们并创建了这个程序.
**MainActivity.java code**
public class MainActivity extends Activity {
RelativeLayout containerLayout;
static int totalEditTexts = 0;
Button button;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
containerLayout = (RelativeLayout)findViewById(R.id.relative1);
button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
totalEditTexts++;
if (totalEditTexts > 100)
return;
EditText editText = new EditText(getBaseContext());
containerLayout.addView(editText);
editText.setGravity(Gravity.RIGHT);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) editText.getLayoutParams();
layoutParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
layoutParams.setMargins(23, 34, 0, 0);
// RelativeLayout.LayoutParams()
editText.setLayoutParams(layoutParams);
//if you want to identify the created editTexts, set a tag, like below
editText.setTag("EditText" + totalEditTexts);
}
});
Run Code Online (Sandbox Code Playgroud)
}}
当我运行此代码时,它会在单击按钮时创建EditText.但只有一次.我不知道之后发生了什么.是创建新的EditText还是重叠旧的EditText,否则它不会创建多个EditText.
任何人都可以解释我现在必须做什么来解决这个问题.
使用RelativeLayout并动态添加内容的问题是,如果您未设置视图的相对位置,则它会相互重叠.因此,当您单击并添加新视图时,将添加新视图,但它们会相互重叠.但是添加LinearLayout,视图可以在彼此下方显示,因为您可以将方向设置为垂直.
归档时间: |
|
查看次数: |
8506 次 |
最近记录: |