我想问一下点击按钮后如何添加textview和edittext?我添加textview和edittext的按钮是“Button addStaff”
这是我的代码:
_12_EventAssign.java
public class _12_EventAssign extends AppCompatActivity {
LinearLayout linearL;
TextView tvdept1;
EditText etdept1;
TextView tvheadofdept1;
EditText etheadofdept1;
View lineview;
Button addStaff;
LinearLayout staff;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity__12__event_assign);
getSupportActionBar().setTitle("Add Event: Information");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
linearL = (LinearLayout)findViewById(R.id.ev_assign_linearlay);
tvdept1 = (TextView)findViewById(R.id.textv_dept1);
etdept1 = (EditText)findViewById(R.id.editT_dept1);
tvheadofdept1 = (TextView)findViewById(R.id.textv_headofdept1);
etheadofdept1 = (EditText)findViewById(R.id.editT_headofdept1);
lineview = findViewById(R.id.view1);
// staff = (LinearLayout)findViewById(R.id.add_staff);
addStaff = (Button)findViewById(R.id.btnaddstaff);
addStaff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearL.addView(tvdept1);
}
});
}
}
这是我的xml文件
activity__12__event_assign.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ortegapatriciaa.enventer._12_EventAssign"
android:background="@color/colorWhite"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ev_assign_linearlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/add_staff">
<TextView
android:id="@+id/textv_dept1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="25dp"
android:text="Department"
android:textColor="@color/colorBlack"
android:textSize="23dp" />
<EditText
android:id="@+id/editT_dept1"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginLeft="35dp"
android:layout_marginTop="7dp"
android:background="@color/colorGray"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="@+id/textv_headofdept1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Head of the Department"
android:textColor="@color/colorBlack"
android:textSize="23dp" />
<EditText
android:id="@+id/editT_headofdept1"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginLeft="35dp"
android:layout_marginTop="7dp"
android:background="@color/colorGray"
android:ems="10"
android:inputType="textPersonName" />
<View
android:id="@+id/view1"
android:layout_width="330dp"
android:layout_height="1dip"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:background="@color/colorBlack" />
</LinearLayout>
<Button
android:id="@+id/btnaddstaff"
android:layout_width="130dp"
android:layout_height="45dp"
android:text="Add Staff"
android:textStyle="bold"
android:textAllCaps="false"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:height="60dp"
android:background="@color/colorWhite"
android:drawablePadding="6dp"
android:drawableLeft="@drawable/ic_person_add_black_24dp"
android:gravity="left|center"
android:padding="6dp"
android:textColor="#000"
android:textSize="18dp"
android:layout_marginTop="5dp"
/>
<Button
android:id="@+id/btnsave"
android:layout_width="250dp"
android:layout_height="40dp"
android:text="Save"
android:textAllCaps="false"
android:textSize="22dp"
android:background="@color/buttonColor"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
/>
</LinearLayout>
</ScrollView>
我希望你能帮助我。谢谢!
答案 0 :(得分:1)
这完美地运作......
您可以根据需要更改 lparams
LinearLayout linearLayout =(LinearLayout)findViewById(R.id.yourLinearLayoutID);
Button yourButton = (Button)findViewById(R.id.yourButtonID);
yourButtonID.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutParams layoutparam = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView textview=new TextView(this);
textview.setLayoutParams(lparams);
textview.setText("Your Text Here"); //For TextView
this.linearLayout.addView(textView);
EditText editText=new EditText(this);
editText.setLayoutParams(lparams);
editText.setText("Your Text Here"); //For EditText
this.linearLayout.addView(editText);
}
});
答案 1 :(得分:0)
您无法将现有视图添加到布局。您必须创建新的TextView并添加:
addStaff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv = new TextView(getApplicationContext());
tv.setBackgroundColor(Color.GRAY);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100);
linearL.addView(tv, params);
}
});
答案 2 :(得分:0)
试试这种方式
addStaff = (Button)findViewById(R.id.btnaddstaff);
addStaff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv = new TextView(this)
tv.setText("yourtext")
EditText et = new Edittext(this)
et.setText("yourtext")
linearL.addView(tv);
linearL.addView(et);
}
});