我如何在 android studio 中仅通过一个按钮创建多个编辑文本
How I can create several Edit texts in android studio only with a button
我只想按一次按钮创建 N 个编辑文本。例如,我输入编辑文本的数量=20[在此处输入图像描述][1],当我按下按钮时,在多个行和列中创建了所有 20 个编辑文本。
有什么解决办法吗??
更好解决方案的示例图片[1]:https://i.stack.imgur.com/wevZN.png
我的代码在下面
String Type,number,tedad;
TextView pazireshnum;
EditText tedadbattri;
String id=ID;
private LinearLayout mLayout;
private EditText mEditText;
private ImageButton mButton;
int k = -1;
int flag,i;
int hint=1;
ArrayList<String> applnserverinstnos = new ArrayList<String>();
public static EditText textView[] = new EditText[100];
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_list);
mLayout = (LinearLayout) findViewById(R.id.ln1);
// mEditText = (EditText) findViewById(R.id.editText);
mButton = (ImageButton) findViewById(R.id.imgbtn);
tedadbattri=(EditText) findViewById(R.id.ed7);
tedadbattri.setText("0");
pazireshnum=(TextView)findViewById(R.id.tv4);
tedad= tedadbattri.getText().toString();
int Tedad = Integer.parseInt(tedad);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
k++;
flag = k;
final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lparams.setMargins(5, 10, 5, 10);
textView[flag] = new EditText(CheckList.this);
textView[flag].setLayoutParams(lparams);
textView[flag].setId(flag);
textView[flag].setPadding(5, 5, 5, 5);
} catch (Exception e) {
e.printStackTrace();
}
mLayout.addView(textView[flag]);
textView[flag].setHint("باتری"+hint);
hint++;
textView[flag].setId(hint);
textView[flag].setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
textView[flag].setBackgroundResource(R.drawable.border4);
}
});
您可以使用 for
循环并在该循环中创建 EditText
并将它们添加到 FlowLayout
容器;也许 this implementation or ConstraintLayout's Flow
你可以检查一下这个程序,假设20可以分成4列5行。所以 运行 for loop
在列和行上添加 editText
视图到 TableRow
和 TableLayout
.
示例 main_activity.xml
使用 TableLayout
添加 EditText 。
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageButton
android:id="@+id/imgbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="137dp"
tools:layout_editor_absoluteY="283dp" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/table">
</TableLayout>
</LinearLayout>
现在试试代码 -
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int total = 20; // quantity of edit texts 20
final int columns = 4;
final int rows = total / columns; // in this case rows would be 5
TableLayout myTable = findViewById(R.id.table);
//Params for TableRow and TableLayout
final TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
final TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1);
//apply to clear
myTable.removeAllViews();
for (int i = 0; i < rows; i++) {
TableRow tableRows = new TableRow(getApplicationContext());
for (int j = 0; j < columns; j++) {
EditText editText = new EditText(getApplicationContext());
editText.setText("voltage" + total--);
tableRows.addView(editText, rowParams);
}
myTable.addView(tableRows, tableParams);
}
}
});
输出-
我只想按一次按钮创建 N 个编辑文本。例如,我输入编辑文本的数量=20[在此处输入图像描述][1],当我按下按钮时,在多个行和列中创建了所有 20 个编辑文本。
有什么解决办法吗??
更好解决方案的示例图片[1]:https://i.stack.imgur.com/wevZN.png
我的代码在下面
String Type,number,tedad;
TextView pazireshnum;
EditText tedadbattri;
String id=ID;
private LinearLayout mLayout;
private EditText mEditText;
private ImageButton mButton;
int k = -1;
int flag,i;
int hint=1;
ArrayList<String> applnserverinstnos = new ArrayList<String>();
public static EditText textView[] = new EditText[100];
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_list);
mLayout = (LinearLayout) findViewById(R.id.ln1);
// mEditText = (EditText) findViewById(R.id.editText);
mButton = (ImageButton) findViewById(R.id.imgbtn);
tedadbattri=(EditText) findViewById(R.id.ed7);
tedadbattri.setText("0");
pazireshnum=(TextView)findViewById(R.id.tv4);
tedad= tedadbattri.getText().toString();
int Tedad = Integer.parseInt(tedad);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
k++;
flag = k;
final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lparams.setMargins(5, 10, 5, 10);
textView[flag] = new EditText(CheckList.this);
textView[flag].setLayoutParams(lparams);
textView[flag].setId(flag);
textView[flag].setPadding(5, 5, 5, 5);
} catch (Exception e) {
e.printStackTrace();
}
mLayout.addView(textView[flag]);
textView[flag].setHint("باتری"+hint);
hint++;
textView[flag].setId(hint);
textView[flag].setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
textView[flag].setBackgroundResource(R.drawable.border4);
}
});
您可以使用 for
循环并在该循环中创建 EditText
并将它们添加到 FlowLayout
容器;也许 this implementation or ConstraintLayout's Flow
你可以检查一下这个程序,假设20可以分成4列5行。所以 运行 for loop
在列和行上添加 editText
视图到 TableRow
和 TableLayout
.
示例 main_activity.xml
使用 TableLayout
添加 EditText 。
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageButton
android:id="@+id/imgbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="137dp"
tools:layout_editor_absoluteY="283dp" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/table">
</TableLayout>
</LinearLayout>
现在试试代码 -
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int total = 20; // quantity of edit texts 20
final int columns = 4;
final int rows = total / columns; // in this case rows would be 5
TableLayout myTable = findViewById(R.id.table);
//Params for TableRow and TableLayout
final TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
final TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1);
//apply to clear
myTable.removeAllViews();
for (int i = 0; i < rows; i++) {
TableRow tableRows = new TableRow(getApplicationContext());
for (int j = 0; j < columns; j++) {
EditText editText = new EditText(getApplicationContext());
editText.setText("voltage" + total--);
tableRows.addView(editText, rowParams);
}
myTable.addView(tableRows, tableParams);
}
}
});
输出-