我在MainActivity中有这个方法,我想在片段上的按钮中使用它们,但我不知道如何将片段中import sys
from time import sleep
words = "#Project_Blitz_v1.5"
for char in words:
sleep(0.03)
sys.stdout.write(char)
sys.stdout.flush()
sleep (0.5)
fb = "..."
for char in fb:
sleep(0.03)
sys.stdout.write(char)
sys.stdout.flush()
words = "eyyyyy"
for char in words:
sleep(0.03)
sys.stdout.write(char)
sys.stdout.flush()
的数据发送到活动并使用按钮。
EditText
我在片段类中有这个,但我不知道如何传递EditTexts值。
public void alta(View v) {
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this);
SQLiteDatabase bd = admin.getWritableDatabase();
String nome = et1.getText().toString();
String edad = et2.getText().toString();
String geno = et3.getText().toString();
String curp = et4.getText().toString();
ContentValues registro = new ContentValues();
registro.put("nombre", nome);
registro.put("edad", edad);
registro.put("genero", geno);
registro.put("curp", curp);
bd.insert("usuarios", null, registro);
bd.close();
et1.setText("");
et2.setText("");
et3.setText("");
et4.setText("");
Toast.makeText(this, "Se cargaron los datos",
Toast.LENGTH_SHORT).show();
}
public void modificacion(View v) {
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this);
SQLiteDatabase bd = admin.getWritableDatabase();
String nome = et1.getText().toString();
String edad = et2.getText().toString();
String geno = et3.getText().toString();
String curp = et4.getText().toString();
ContentValues registro = new ContentValues();
registro.put("nombre", nome);
registro.put("edad", edad);
registro.put("genero", geno);
registro.put("curp", curp);
int cant = bd.update("usuarios", registro, "curp=" + curp, null);
bd.close();
if (cant == 1)
Toast.makeText(this, "se modificaron los datos", Toast.LENGTH_SHORT)
.show();
else
Toast.makeText(this, "no existe un usuario con ese nombre",
Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:0)
首先,重要的是Activity不会尝试直接访问片段内部的视图(这最初会起作用,但在边缘情况下会因无效引用而导致崩溃)。我建议的是在MainActivity中创建一个接口,Fragment将通知Activity。
public class MainActivity {
String textArr[4] = new String[4];
public void setEditTextValue(String text, int id) {
textArr[id] = text;
}
}
public class Fragment1 extends Fragment {
public void updateData() {
MainActivity main = (MainActivity)getActivity();
main.setEditTextValue(iet1.getText().toString(), 0);
main.setEditTextValue(iet2.getText().toString(), 1);
main.setEditTextValue(iet3.getText().toString(), 2);
main.setEditTextValue(iet4.getText().toString(), 3);
main.modificacion(); // Which doesn't need view and can just use the data saved in the array
}
}