To clear the textview I use

textView_vivod.setText(""); 

Also probyval

 textView_vivod.setText(null); 

The test field is not cleared before pasting text.

 String text = String.valueOf(editText_text.getText());//получаем текст int key = Integer.parseInt(editText_num.getText().toString());//получаем число for (int i = 0; i < text.length(); i++) // Построение зашифрованной строки сообщения, encmsg = encmsg + (char) (text.charAt(i) ^ key); //очищаем текстовое поле textView_vivod.setText(""); //textView_vivod.setText(null); //вставляем зашифрованный текст textView_vivod.setText(encmsg); 

Full code

 import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { EditText editText_text; EditText editText_num; TextView textView_vivod; Button button; String encmsg = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText_num = (EditText) findViewById(R.id.editText_vvod_kod); editText_text = (EditText) findViewById(R.id.edit_text_vvod_text); textView_vivod = (TextView) findViewById(R.id.textView_zahifrovanoe); button = (Button) findViewById(R.id.button_zashifrovat); } public void Zashifrovat(View view) { if (editText_text.getText().toString().equals("")) { // Здесь код, если editText_text пуст Toast toast = Toast.makeText(getApplicationContext(), "Введите фразу!", Toast.LENGTH_LONG); toast.show(); // Здесь код, если editText_num пуст } else if (editText_num.getText().toString().equals("")) { Toast toast = Toast.makeText(getApplicationContext(), "Введите число-пороль!", Toast.LENGTH_LONG); toast.show(); } else { String text = String.valueOf(editText_text.getText());//получаем текст int key = Integer.parseInt(editText_num.getText().toString());//получаем число for (int i = 0; i < text.length(); i++) // Построение зашифрованной строки сообщения, encmsg = encmsg + (char) (text.charAt(i) ^ key); //очищаем текстовое поле textView_vivod.setText(""); //textView_vivod.setText(null); //вставляем зашифрованный текст textView_vivod.setText(encmsg); } } } 

Thanks in advance for any help.

  • 2
    what does not work? you first see an empty string, and immediately encmsg. What result did you want to see? - Shwarz Andrei
  • But how did you determine that the line is not cleared? =)))) - Android Android
  • 3
    I love names such as Zashifrovat methods, links in the form of R.id.editText_vvod_kod, I will always be sure of my relevance in the market: D ....... As a matter of fact, before (int i = 0; i <text .length (); i ++) write a simple line encmsg = ""; - Chaynik

2 answers 2

Everything is cleaned, you need to clean the variable encmsg.

 encmsg = ""; for (int i = 0; i < text.length(); i++){ encmsg = encmsg + (char) (text.charAt(i) ^ key); } textView_vivod.setText(encmsg); 

Here with this code you all soar

    Damn, how everything turned out to be simple)) Thank you very much !!!))) Special thanks to Chaynik An example is taken from Schildt "Beginner's Guide" by the way ... I did not read carefully)) The names of the methods and stuff are funny, of course, but I'm learning)) But seriously, thank you for taking the time to answer stupid questions ... God bless you with happiness and health))))