Ich arbeite an einer Anwendung, wo ich mehrere EditText
und Spinner dynamisch erstellen muss. So fing ich an, für die Lösung zu suchen, weil ich keine Erlaubnis habe, unsichtbare Eigenschaft in XML-Datei zu verwenden. Ich suchte viel und bekam ein paar Beispiele nur auf stackoverflow. Ich folge ihnen und erstelle dieses Programm.EditText dynamisch in Android erstellen
**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);
}
});
} }
Wenn ich diesen Code leite, es schafft die EditText, wenn die Schaltfläche geklickt wurde. Aber nur einmal. Ich weiß nicht, was danach passiert. ob es einen neuen EditText erstellt oder den alten überlappt, andernfalls erstellt es nicht mehr als einen EditText.
Kann mir jemand erklären, was ich jetzt tun müssen, um dieses Problem zu beheben.