android开发中动态添加EditText控件的方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wpwbb510582246/article/details/52565929

一、基本步骤

1、定义一个RelativeLayout对象

  1. private RelativeLayout rlActivityMain;

2、定义一个EditText对象并将其初始化

  1. private EditText etIntroductImage;
  1. etIntroductImage=new EditText(MainActivity.this);
  2. etIntroductImage.setText("图片说明");

3、定义一个RelativeLayout.LayoutParams对象并将其初始化

  1. private RelativeLayout.LayoutParams tempLayoutParams;
  1. tempLayoutParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 90);//ViewGroup.LayoutParams.WRAP_CONTENT是要添加控件的width属性的值,90是要添加控件的height属性的值

4、设置EditText控件的位置

  1. //imgFengJing的定义为private ImageView imgFengJing;
  2. //将EditText放在imgFengJing上方水平居中的位置
  3. tempLayoutParams.addRule(RelativeLayout.ABOVE, imgFengJing.getId());
  4. tempLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL,imgFengJing.getId());

5、将EditText添加到布局中

  1. rlActivityMain.addView(etIntroductImage, tempLayoutParams);

二、效果图

三、源代码

MainActivity.java

  1. package com.weipeng.adroid.myrelativelayout;
  2.  
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.view.ViewGroup;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.ImageView;
  12. import android.widget.RelativeLayout;
  13. import android.widget.RelativeLayout.LayoutParams;
  14. import android.widget.TextView;
  15.  
  16. public class MainActivity extends Activity implements OnClickListener {
  17.  
  18. private Button btnUp;
  19. private Button btnDown;
  20. private Button btnLeft;
  21. private Button btnRight;
  22. int[] Buttons;
  23. private ImageView imgFengJing;
  24. private EditText etIntroductImage;
  25. private RelativeLayout rlActivityMain;
  26. private RelativeLayout.LayoutParams tempLayoutParams;
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_main);
  31. System.out.println("已执行:setContentView");
  32. btnUp=(Button) findViewById(R.id.btnUp);
  33. btnDown=(Button) findViewById(R.id.btnDown);
  34. btnLeft=(Button) findViewById(R.id.btnLeft);
  35. btnRight=(Button) findViewById(R.id.btnRight);
  36. System.out.println("已执行:(Button) findViewById");
  37. rlActivityMain=(RelativeLayout) findViewById(R.id.rlActivityMain);
  38. imgFengJing=(ImageView) findViewById(R.id.imgFengJing);
  39. System.out.println("已执行:(RelativeLayout) findViewById");
  40. System.out.println("已执行:(ImageView) findViewById");
  41. btnUp.setOnClickListener(this);
  42. btnDown.setOnClickListener(this);
  43. btnLeft.setOnClickListener(this);
  44. btnRight.setOnClickListener(this);
  45. System.out.println("已执行:setOnClickListener");
  46. }
  47.  
  48. @Override
  49. public boolean onCreateOptionsMenu(Menu menu) {
  50. getMenuInflater().inflate(R.menu.main, menu);
  51. return true;
  52. }
  53.  
  54. @Override
  55. public void onClick(View v) {
  56. etIntroductImage=new EditText(MainActivity.this);
  57. etIntroductImage.setText("图片说明");
  58. tempLayoutParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 90);
  59. switch (v.getId()) {
  60. case R.id.btnUp:
  61. tempLayoutParams.addRule(RelativeLayout.ABOVE, imgFengJing.getId());
  62. tempLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL,imgFengJing.getId());
  63. rlActivityMain.addView(etIntroductImage, tempLayoutParams);
  64. break;
  65. case R.id.btnDown:
  66. tempLayoutParams.addRule(RelativeLayout.BELOW, imgFengJing.getId());
  67. tempLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, imgFengJing.getId());
  68. rlActivityMain.addView(etIntroductImage, tempLayoutParams);
  69. break;
  70. case R.id.btnLeft:
  71. tempLayoutParams.addRule(RelativeLayout.LEFT_OF,imgFengJing.getId());
  72. tempLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, imgFengJing.getId());
  73. rlActivityMain.addView(etIntroductImage, tempLayoutParams);
  74. break;
  75. case R.id.btnRight:
  76. tempLayoutParams.addRule(RelativeLayout.RIGHT_OF, imgFengJing.getId());
  77. tempLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, imgFengJing.getId());
  78. rlActivityMain.addView(etIntroductImage, tempLayoutParams);
  79. break;
  80. }
  81. System.out.println("已执行:onClick");
  82. }
  83.  
  84. }

activity_main_xml

 
 
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:id="@+id/rlActivityMain"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:background="#ffa500" >
  7.  
  8.     <Button
  9.         android:id="@+id/btnUp"
  10.         android:layout_width="wrap_content"
  11.         android:layout_height="wrap_content"
  12.         android:layout_alignParentTop="true"
  13.         android:layout_centerHorizontal="true"
  14.         android:text="@string/上" />
  15.  
  16.     <Button
  17.         android:id="@+id/btnDown"
  18.         android:layout_width="wrap_content"
  19.         android:layout_height="wrap_content"
  20.         android:layout_alignParentBottom="true"
  21.         android:layout_centerHorizontal="true"
  22.         android:text="@string/下" />
  23.  
  24.     <Button
  25.         android:id="@+id/btnLeft"
  26.         android:layout_width="wrap_content"
  27.         android:layout_height="wrap_content"
  28.         android:layout_alignParentLeft="true"
  29.         android:layout_centerVertical="true"
  30.         android:text="@string/左" />
  31.  
  32.     <Button
  33.         android:id="@+id/btnRight"
  34.         android:layout_width="wrap_content"
  35.         android:layout_height="wrap_content"
  36.         android:layout_alignParentRight="true"
  37.         android:layout_centerVertical="true"
  38.         android:text="@string/右" />
  39.  
  40.     <ImageView
  41.         android:id="@+id/imgFengJing"
  42.         android:layout_width="33dp"
  43.         android:layout_height="33dp"
  44.         android:layout_centerInParent="true"
  45.         android:src="@drawable/fengjing" />
  46.  
  47. </RelativeLayout>

猜你喜欢

转载自blog.csdn.net/wpwbb510582246/article/details/52565929