setText on button from another activity android
我有一个问题,我想点击列表,调用一个新活动并将按钮重命名为另一个名称。
我尝试了几件事,没有用,有人可以帮帮我吗?
我的班级
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | private AdapterView.OnItemClickListener selecionarTime = new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView arg0, View arg1, int pos, long id) { t = times.get(pos); <strike>CadastroTimes cad = new CadastroTimes();</strike> <strike>CadastroTimes.salvar.setText("Alterar");</strike> Intent intent = new Intent(EditarTimes.this, CadastroTimes.class); startActivity(intent); } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | public class CadastroTimes extends AppCompatActivity { private Time t; private timeDatabase db; private EditText edID; private EditText edNome; public Button salvar; @Override 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("Cadastrar"); String newString; if (savedInstanceState == null) { Bundle extras = getIntent().getExtras(); if(extras == null) { newString= null; } else { newString= extras.getString("Alterar"); } } else { newString= (String) savedInstanceState.getSerializable("Alterar"); } //button in CadastroTimes activity to have that String as text System.out.println(newString +" AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); salvar.setText(newString); } public void salvarTime(View v) { t = new Time(); t.setNome(edNome.getText().toString()); if (salvar.getText().equals("Alterar")) { db.atualizar(t); exibirMensagem("Time atualizado com sucesso!"); } else { db.salvar(t); exibirMensagem("Time cadastrado com sucesso!"); } Intent intent = new Intent(this, EditarTimes.class); startActivity(intent); } private void limparDados() { edID.setText(""); edNome.setText(""); edNome.requestFocus(); } private void exibirMensagem(String msg) { Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | public class EditarTimes extends AppCompatActivity { private Time t; private List times; private timeDatabase db; private ListView lvTimes; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_editar_times); lvTimes = (ListView) findViewById(R.id.lvTimes); lvTimes.setOnItemClickListener(selecionarTime); lvTimes.setOnItemLongClickListener(excluirTime); times = new ArrayList(); db = new timeDatabase(getApplicationContext()); atualizarLista(); } private void excluirTime(final int idTime) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Excluir time?") .setIcon(android.R.drawable.ic_dialog_alert) .setMessage("Deseja excluir esse time?") .setCancelable(false) .setPositiveButton(getString(R.string.sim), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { if (db.deletar(idTime)) { atualizarLista(); exibirMensagem(getString(R.string.msgExclusao)); } else { exibirMensagem(getString(R.string.msgFalhaExclusao)); } } }) .setNegativeButton(getString(R.string.nao), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); builder.create(); builder.show(); atualizarLista(); } private void atualizarLista() { times = db.listAll(); if (times != null) { if (times.size() > 0) { TimeListAdapter tla = new TimeListAdapter( getApplicationContext(), times); lvTimes.setAdapter(tla); } } } private AdapterView.OnItemClickListener selecionarTime = new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView arg0, View arg1, int pos, long id) { t = times.get(pos); Intent intent = new Intent(EditarTimes.this, CadastroTimes.class); String strName ="Alterar"; intent.putExtra("Alterar", strName); startActivity(intent); //preecherDados(t); } }; private AdapterView.OnItemLongClickListener excluirTime = new AdapterView.OnItemLongClickListener() { public boolean onItemLongClick(AdapterView arg0, View arg1, int pos, long arg3) { excluirTime(times.get(pos).getId()); return true; } }; /*private void preecherDados(Time time) { edID.setText(String.valueOf(time.getId())); edNome.setText(time.getNome()); }*/ private void exibirMensagem(String msg) { Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); } public void telaCadastrar(View view) { Intent intent = new Intent(this, CadastroTimes.class); startActivity(intent); } public void botaoSair(View view) { Intent intent = new Intent(this, TelaInicial.class); startActivity(intent); } } |
您可以将按钮标题传递给
1 2 3 | Intent intent = new Intent(EditarTimes.this, CadastroTimes.class); intent.putExtra("buttontxt","Changed Text"); startActivity(intent); |
然后在
1 2 3 4 5 6 7 | 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); |
记得在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | //From Activity Intent intent = new Intent(EditarTimes.this, CadastroTimes.class); intent.pueExtra("change_tag","text to change"); startActivity(intent) //To Activity public void onCreate(..){ Button changeButton = (Button)findViewById(R.id.your_button); // Button to set received text Intent intent = getIntent(); if(null != intent && !TextUtils.isEmpty(intent.getStringExtra("change_tag"))) { String changeText = intent.getStringExtra("change_tag"); // Extracting sent text from intent changeButton.setText(changeText); // Setting received text on Button } } |
对不起,我有点晚了。但是,我希望能帮到你。
1:使用
在ActivityOne.class中:
1 2 3 | Intent intent = new Intent(getApplicationContext(), ActivityTwo.class); intent.putExtra("key","value"); intent.startActivity(); |
在ActivityTwo.class中:
1 2 |
2:以编程方式修改按钮文本:
1 | btn_object.setText(var); |
希望对你有帮助
在我的情况下,我必须从
TimerActivity.class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public void buttonClick_timerOK(View view) { // Identify the (EditText) for reference: EditText editText_timerValue; editText_timerValue = (EditText) findViewById(R.id.et_timerValue); // Required 'if' statement (to avoid NullPointerException): if (editText_timerValue != null) { // Continue with Button code.. // Convert value of the (EditText) to a (String) String string_timerValue; string_timerValue = editText_timerValue.getText().toString(); // Declare Intent for starting the Service Intent intent = new Intent(this, TimerService.class); // Add Intent-Extras as data from (EditText) intent.putExtra("TIMER_VALUE", string_timerValue); // Start Service startService(intent); // Close current Activity finish(); } else { Toast.makeText(TimerActivity.this,"Please enter a Value!", Toast.LENGTH_LONG).show(); } } |
然后在我的Service类中,我检索了值,并在
TimerService.class
1 2 3 4 5 6 7 8 9 10 | // Retrieve the user-data from (EditText) in TimerActivity intent.getStringExtra("TIMER_VALUE"); // IS THIS NEEDED, SINCE ITS ASSIGNED TO A STRING BELOW TOO? // Assign a String value to the (EditText) value you retrieved.. String timerValue; timerValue = intent.getStringExtra("TIMER_VALUE"); // You can also convert the String to an int, if needed. // Now you can reference"timerValue" for the value anywhere in the class you choose. |
希望我的贡献有所帮助!
快乐的编码!
如果你想改变那个值,不要通过意图去活动你可以使用文件来保存值到文件或你有多个值使用数据库和访问
值oncreate设置文本的值....
要更改按钮文本:
现在,我找到了你:
您的
1 2 3 4 5 6 7 8 9 10 11 12 | //set setOnItemClickListener youtListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView< ? > parent, View view,int position, long id) { Intent i = new Intent(EditarTimes.this, CadastroTimes.class); //text which you want to display on the button to CadastroTimes activity String strName ="hello button"; i.putExtra("STRING_I_NEED", strName); } }); |
在
在
1 2 3 4 5 6 7 8 9 10 11 |
//在CadastroTimes活动中按钮将该String作为文本
yourButton.setText(newString);
好的,所以第一步是获取你想要的按钮并使它成为一个公共静态对象(并将它放在类的顶部)。
1 |
然后你可以在另一个类中使用它来操纵它:
1 | ClassName.button.setText("My Button"); |
在你的情况下它是
1 | CadastroTimes.salvar.setText("Alterar"); |