gpt4 book ai didi

java - 如何从我从 JavaClass 创建的 EditText 中获取文本

转载 作者:太空狗 更新时间:2023-10-29 13:02:49 25 4
gpt4 key购买 nike

我是 Android Studio 中的新手,我正在尝试从我从 java 类创建的 EditText 中获取文本,而不是在 xml 中可视化。该项目的主要思想是尝试做一个数独,所以我有:

  • 9x9 table
  • 81个数的数组
  • 表格的一些方 block 有 EditText,其他方 block 有 TextView(如数独)

But the problem is I dont know how to do a getText from the EditText because they are not created in a normal way so i cant do "r.id.edittext_1" for example.

还有其他方法获取文本吗?

我的文件:

tabla.java(我在其中创建表格以及所有 EditText 和 TextView)

public class Tabla{
// Variables de la clase

private TableLayout tabla; // Layout donde se pintará la tabla
private ArrayList<TableRow> filas; // Array de las filas de la tabla
private Activity actividad;
private Resources rs;
private int FILAS, COLUMNAS; // Filas y columnas de nuestra tabla

/**
* Constructor de la tabla
* @param actividad Actividad donde va a estar la tabla
* @param tabla TableLayout donde se pintará la tabla
*/
public Tabla(Activity actividad, TableLayout tabla)
{
this.actividad = actividad;
this.tabla = tabla;
rs = this.actividad.getResources();
FILAS = COLUMNAS = 0;
filas = new ArrayList<TableRow>();
}

/**
* Añade la cabecera a la tabla
* @param recursocabecera Recurso (array) donde se encuentra la cabecera de la tabla
*/
public void agregarCabecera(int recursocabecera)
{
TableRow.LayoutParams layoutCelda;
TableRow fila = new TableRow(actividad);
TableRow.LayoutParams layoutFila = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
fila.setLayoutParams(layoutFila);

String[] arraycabecera = rs.getStringArray(recursocabecera);
COLUMNAS = arraycabecera.length;

for(int i = 0; i < arraycabecera.length; i++)
{
TextView texto = new TextView(actividad);
layoutCelda = new TableRow.LayoutParams(obtenerAnchoPixelesTexto(arraycabecera[i]), TableRow.LayoutParams.WRAP_CONTENT);
texto.setText(arraycabecera[i]);
texto.setGravity(Gravity.CENTER_HORIZONTAL);
texto.setTextAppearance(actividad, R.style.estilo_celda);
//texto.setBackgroundResource(R.drawable.tabla_celda_cabecera);
texto.setLayoutParams(layoutCelda);

fila.addView(texto);
}

tabla.addView(fila);
filas.add(fila);

FILAS++;
}

/**
* Agrega una fila a la tabla
* @param elementos Elementos de la fila
*/
public void agregarFilaTabla(ArrayList<Integer> elementos)
{
TableRow.LayoutParams layoutCelda;
TableRow.LayoutParams layoutFila = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
TableRow fila = new TableRow(actividad);
fila.setLayoutParams(layoutFila);

for(int i = 0; i< elementos.size(); i++)
{
//Log.v("RESULTADO","X:"+String.valueOf(elementos.get(i)));
if(elementos.get(i) == 0){
EditText texto = new EditText(actividad); //FUNCION PARA PODER EDITAR
//texto.setText(String.valueOf(elementos.get(i))); //FUNCION PARA ESCRIBIR NUMERO ARRAY
//texto.getText().clear(); //FUNCION PARA BORRAR EL TEXTO
texto.setGravity(Gravity.CENTER_HORIZONTAL); //FUNCION PARA ALINEAR
texto.setInputType(2); //FUNCION PARA QUE SE PUEDA EDITAR CON NUMEROS SOLO
texto.setTextAppearance(actividad, R.style.estilo_editables); //FUNCION PARA CARGAR EL COLOR DEL TEXTO
texto.setFilters(new InputFilter[] {new InputFilter.LengthFilter(1)}); //FUNCION PARA LIMITAR LA CANTIDAD DE TEXTO
texto.setBackgroundResource(R.drawable.tabla_celda);
layoutCelda = new TableRow.LayoutParams(obtenerAnchoPixelesTexto(texto.getText().toString()), TableRow.LayoutParams.WRAP_CONTENT);
texto.setLayoutParams(layoutCelda);
fila.addView(texto);
}else{
TextView texto = new TextView(actividad);
texto.setText(String.valueOf(elementos.get(i)));
texto.setGravity(Gravity.CENTER_HORIZONTAL);
//texto.setInputType(0); //FUNCION PARA QUE NO SE PUEDA EDITAR, SOLO APLICAR A EDITTEXT
texto.setTextAppearance(actividad, R.style.estilo_celda);
texto.setBackgroundResource(R.drawable.tabla_celda);
layoutCelda = new TableRow.LayoutParams(obtenerAnchoPixelesTexto(texto.getText().toString()), TableRow.LayoutParams.WRAP_CONTENT);
texto.setLayoutParams(layoutCelda);
fila.addView(texto);
}

}

tabla.addView(fila);
filas.add(fila);

FILAS++;
}

/**
* Elimina una fila de la tabla
* @param indicefilaeliminar Indice de la fila a eliminar
*/
public void eliminarFila(int indicefilaeliminar)
{
if( indicefilaeliminar > 0 && indicefilaeliminar < FILAS )
{
tabla.removeViewAt(indicefilaeliminar);
FILAS--;
}
}

/**
* Devuelve las filas de la tabla, la cabecera se cuenta como fila
* @return Filas totales de la tabla
*/
public int getFilas()
{
return FILAS;
}

/**
* Devuelve las columnas de la tabla
* @return Columnas totales de la tabla
*/
public int getColumnas()
{
return COLUMNAS;
}

/**
* Devuelve el número de celdas de la tabla, la cabecera se cuenta como fila
* @return Número de celdas totales de la tabla
*/
public int getCeldasTotales()
{
return FILAS * COLUMNAS;
}

/**
* Obtiene el ancho en píxeles de un texto en un String
* @param texto Texto
* @return Ancho en píxeles del texto
*/
private int obtenerAnchoPixelesTexto(String texto)
{
Paint p = new Paint();
Rect bounds = new Rect();
p.setTextSize(50);

p.getTextBounds(texto, 0, texto.length(), bounds);
return 100;
}

public int compararSolucion(ArrayList[] bueno , ArrayList[] usuario){
if(bueno == usuario){
return 1; //Correcto
}else{
return 0; //Malo
}
}
}

nivel1.java( Activity 类)

public class nivel1 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nivel1);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

ArrayList<Integer> suddoku = new ArrayList<Integer>();
Integer[] otherList = new Integer[] {0,0,0,0,0,0,0,8,4,5,0,0,0,4,2,6,0,0,0,0,4,0,0,0,0,2,0,0,4,0,0,6,3,7,0,0,0,0,0,0,0,1,0,0,3,6,3,0,9,5,7,2,0,0,0,5,0,0,0,9,0,0,6,3,2,0,8,0,0,1,0,9,0,0,9,5,0,0,8,0,0};
Integer[] correctList = new Integer[] {7,9,2,6,1,5,3,8,4,5,8,3,7,4,2,6,9,1,1,6,4,3,9,8,5,2,7,9,4,8,2,6,3,7,1,5,2,7,5,4,8,1,9,6,3,6,3,1,9,5,7,2,4,8,8,5,7,1,2,9,4,3,6,3,2,6,8,7,4,1,5,9,4,1,9,5,3,6,8,7,2};
suddoku.addAll(Arrays.asList(otherList));

Tabla tabla = new Tabla(this, (TableLayout)findViewById(R.id.tabla));
tabla.agregarCabecera(R.array.cabecera_tabla);
int x = 0;
int contador = 0;
for(int i = 0; i < 9; i++)
{
ArrayList<Integer> elementos = new ArrayList<Integer>();
for(x = x; contador < 9; contador++){
elementos.add(suddoku.get(x));
x++;
}
contador = 0;

tabla.agregarFilaTabla(elementos);


}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_sudokus, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.resolvertop) {
return true; //I want when you push this button, you get the text from all the EditText and put in an array, then compare with the complete array.
}

return super.onOptionsItemSelected(item);
}


public boolean onSupportNavigateUp() {
onBackPressed();
return false;
}
}

content_nivel1.xml( Activity 布局)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".nivel1"
tools:showIn="@layout/activity_nivel1">

<LinearLayout 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"
android:orientation="vertical"
android:gravity="center_horizontal">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="8"
android:id="@+id/layoutTexto">

</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/layoutTabla"
android:gravity="center">

<ScrollView
android:id="@+id/scrollvertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_weight="1">

<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollhorizontal"
android:scrollbars="horizontal"
android:layout_weight="1">

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout
android:id="@+id/tabla"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableLayout>
</LinearLayout>

</HorizontalScrollView>
</ScrollView>

</LinearLayout>

</LinearLayout>

</android.support.constraint.ConstraintLayout>

解决方案

将此函数添加到 Tabla 类

 public String returnText(String tag){
EditText texto = (EditText)tabla.findViewWithTag(tag);

return texto.getText().toString();
}

然后从 nivel1 类中调用它

 String texto = tabla.returnText(tag);

Thank you all for your time and help.

最佳答案

当您使用行/列创建 EditText setTag 时。

EditText texto = new EditText(actividad);
textTo.setTag(row+col);

当你需要 getText() 时

EditText texto = (EditText)tabla.findViewWithTag(row+col);
textto.getText()

关于java - 如何从我从 JavaClass 创建的 EditText 中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53169894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com