java - 来自另一个 Activity android的按钮上的setText

标签 java android android-intent settext

我有一个问题,我想单击列表,调用一个新 Activity 并将按钮重命名为另一个名称。

我尝试了几件事,没有任何效果,有人可以帮助我吗?

我的类EditarTimes :

私有(private) AdapterView.OnItemClickListener 选择时间 = 新 AdapterView.OnItemClickListener() {

public void onItemClick(AdapterView arg0, View arg1, int pos, long id) {
t = 时间.get(pos);

CadastroTimes cad = new CadastroTimes();
CadastroTimes.salvar.setText("Alterar");
Intent intent = new Intent(EditarTimes.this, CadastroTimes.class);
开始 Activity ( Intent );

}

};

公共(public)类 CadastroTimes 扩展 AppCompatActivity {

私有(private)时间 t;
私有(private)时间数据库数据库;
私有(private) EditText edID;
私有(private) EditText edNome;
公共(public)按钮萨尔瓦;

@覆盖
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cadastro_times);

edID = (EditText) findViewById(R.id.edID);
edNome = (EditText) findViewById(R.id.edNome);
db = new timeDatabase(getApplicationContext());
Salvar = (Button) findViewById(R.id.btnCadastrar);
salvar.setText("地籍");
字符串 newString;
if (savedInstanceState == null) {
bundle 额外费用 = getIntent().getExtras();
如果(额外 == 空){
新字符串=空;
} 别的 {
newString= extras.getString("Alterar");
}
} 别的 {
newString= (String) savedInstanceState.getSerializable("Alterar");
}

//CadastroTimes Activity 中的按钮将该字符串作为文本
System.out.println(newString + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
Salvar.setText(newString);
}

公共(public)无效萨尔瓦时间(查看 v){
t = 新时间();
t.setNome(edNome.getText().toString());

if (salvar.getText().equals("Alterar")) {
db.atualizar(t);
exibirMensagem("时间结束!");
} 别的 {
db.salvar(t);
exibirMensagem("时间地籍成功!");
}

Intent intent = new Intent(this, EditarTimes.class);
开始 Activity ( Intent );

}

私有(private)无效limparDados(){
edID.setText("");
edNome.setText("");
edNome.requestFocus();
}

私有(private)无效 exibirMensagem(字符串消息){
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}

}

公共(public)类 EditarTimes 扩展 AppCompatActivity {

私有(private)时间 t;
私有(private)名单时间;
私有(private)时间数据库数据库;
私有(private) ListView lvTimes;

@覆盖
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editar_times);

lvTimes = (ListView) findViewById(R.id.lvTimes);
lvTimes.setOnItemClickListener(选择时间);
lvTimes.setOnItemLongClickListener(excluirTime);
时间 = 新 ArrayList();
db = new timeDatabase(getApplicationContext());
atualizarLista();
}

私有(private)无效excluirTime(最终int idTime){

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("独家时间?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage("Deseja excluir esse 时间?")
.setCancelable(false)
.setPositiveButton(getString(R.string.sim),
新 DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
如果(db.deletar(idTime)){
atualizarLista();
exibirMensagem(getString(R.string.msgExclusao));
} 别的 {
exibirMensagem(getString(R.string.msgFalhaExclusao));
}

}
})
.setNegativeButton(getString(R.string.nao),
新 DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
对话框取消();
}
});
builder.create();
builder.show();

atualizarLista();

}

私有(private)无效 atualizarLista() {

时间 = db.listAll();
如果(次!= null){
如果(时间。大小()> 0){
TimeListAdapter tla = 新的 TimeListAdapter(
getApplicationContext(), 次);
lvTimes.setAdapter(tla);
}

}

}

私有(private) AdapterView.OnItemClickListener 选择时间 = 新 AdapterView.OnItemClickListener() {

public void onItemClick(AdapterView arg0, View arg1, int pos, long id) {
t = 时间.get(pos);

Intent intent = new Intent(EditarTimes.this, CadastroTimes.class);
String strName = "Alterar";
intent.putExtra("Alterar", strName);
开始 Activity ( Intent );


//preecherDados(t);

}

};

私有(private) AdapterView.OnItemLongClickListener excluirTime = 新 AdapterView.OnItemLongClickListener() {

公共(public) boolean onItemLongClick(AdapterView arg0,查看 arg1,
int pos, long arg3) {
excluirTime(times.get(pos).getId());
返回真;
}

};

/*private void preecherDados(Time time) {
edID.setText(String.valueOf(time.getId()));
edNome.setText(time.getNome());
}*/

私有(private)无效 exibirMensagem(字符串消息){
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}

public void telaCadastrar(查看 View ){
Intent intent = new Intent(this, CadastroTimes.class);
开始 Activity ( Intent );
}

public void botaoSair(查看 View ){
Intent intent = new Intent(this, TelaIncial.class);
开始 Activity ( Intent );
}
}

最佳答案

您可以将按钮标题传递给 CadastroTimes Intent 作为

Intent intent = new Intent(EditarTimes.this, CadastroTimes.class);
intent.putExtra("buttontxt","Changed Text");
startActivity(intent);

然后在 CadastroTimes.java将按钮的文本设置为您传递的新值。代码将如下所示:
button = (Button)findViewById(R.id.button); // This is your reference from the xml. button is my name, you might have your own id given already.
Bundle extras = getIntent().getExtras();
String value = ""; // You can do it in better and cleaner way
if (extras != null) {
    value = extras.getString("buttontxt");
}
button.setText(value);

记得在onCreate做之后 setContentView

关于java - 来自另一个 Activity android的按钮上的setText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33597302/

相关文章:

java - 哪个在内存 : char[] or String? 方面是有效的

java - Android - 打开图库中的文件夹

android单选图像选择器

Android 应用程序无法打开网页链接

java - 最初隐藏的单选按钮在事件中不可见

java - 什么时候 Java 对象可序列化但不可克隆才有意义?

安卓内部存储

java - 具有不同树的同一个 jar 的依赖关系

java - 创建 grails 二进制插件会生成 jar 文件,其中包含所有带注释的 groovy 文件

android - 动态添加edittext并在android中检索值