我正在构建一个应用程序,我有一个加号按钮,每次单击该按钮时,都会添加一个带有文本框的新布局。
答案 0 :(得分:0)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/ln"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:gravity="left"
android:onClick="addAddbutton"
android:text="add"
android:visibility="visible" />
</LinearLayout>
</RelativeLayout>
mainActivity.java
package com.example.textboxonclick;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private LinearLayout ln;
private EditText myEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ln = (LinearLayout) findViewById(R.id.ln);
}
public void addAddbutton(View v) {
myEditText = new EditText(this);
ln.addView(myEditText);
}
}