获取Android中的字符串,逐行编程获取。

huangapple go评论121阅读模式
英文:

Get string line by line android programmatically

问题

以下是您要翻译的部分:

原文:

I have this code but the problem is that this continually showing toast of each string line when I pressed my desired button.

Like this:

Clicked the button once:

Toast shows continually:

A

B

C

What I want is to get string from a single line once I click the button.

Like this:

EditText value:
A

B

C

Result first click:

A only

Result second click:

B only

Result third click:

C only

Like that

Btw this is the code I use:

  1. Button checkButton = (Button) findViewById(R.id.check_Button);
  2. checkButton.setOnClickListener(new View.OnClickListener() {
  3. public void onClick(View v) {
  4. EditText editBox = (EditText) findViewById(R.id.edit_Text);
  5. String items = editBox.getText().toString();
  6. Scanner scanner = new Scanner(items);
  7. while (scanner.hasNextLine()) {
  8. String line = scanner.nextLine();
  9. Toast.makeText(getApplicationContext(), line, Toast.LENGTH_SHORT).show();
  10. }
  11. scanner.close();
  12. }
  13. }
  14. );

I know it's hard to understand but please bare with me.

翻译:

我有这段代码,但问题是每当我按下所需按钮时,它会持续显示每个字符串行的提示。

像这样:

单击按钮一次:

提示不断显示:

A

B

C

我想要的是,一旦我点击按钮,就从单行获取字符串。

像这样:

EditText 值:
A

B

C

第一次点击结果:

仅 A

第二次点击结果:

仅 B

第三次点击结果:

仅 C

就像这样

顺便说一下,这是我使用的代码:

  1. Button checkButton = (Button) findViewById(R.id.check_Button);
  2. checkButton.setOnClickListener(new View.OnClickListener() {
  3. public void onClick(View v) {
  4. EditText editBox = (EditText) findViewById(R.id.edit_Text);
  5. String items = editBox.getText().toString();
  6. Scanner scanner = new Scanner(items);
  7. while (scanner.hasNextLine()) {
  8. String line = scanner.nextLine();
  9. Toast.makeText(getApplicationContext(), line, Toast.LENGTH_SHORT).show();
  10. }
  11. scanner.close();
  12. }
  13. }
  14. );

我知道这很难理解,但请谅解。

英文:

I have this code but the problem is that this continually showing toast of each string line when I pressed my desired button.

Like this:

Clicked the button once:

Toast shows continually:

A

B

C

What I want is to get string from a single line once I click the button.

Like this:

EditText value:
A

B

C

Result first click:

A only

Result second click:

B only

Result third click:

C only

Like that

Btw this is the code I use:

  1. Button checkButton = (Button) findViewById(R.id.check_Button);
  2. checkButton.setOnClickListener(new View.OnClickListener() {
  3. public void onClick(View v) {
  4. EditText editBox = (EditText) findViewById(R.id.edit_Text);
  5. String items = editBox.getText().toString();
  6. Scanner scanner = new Scanner(items);
  7. while (scanner.hasNextLine()) {
  8. String line = scanner.nextLine();
  9. Toast.makeText(getApplicationContext(), line, Toast.LENGTH_SHORT).show();
  10. }
  11. scanner.close();
  12. }
  13. }
  14. );

I know it's hard to understand but please bare with me.

答案1

得分: 0

我认为你可以采用这样的方法。

  1. int clicks = 0;
  2. Button checkButton = (Button) findViewById(R.id.check_Button);
  3. checkButton.setOnClickListener(new View.OnClickListener() {
  4. public void onClick(View v) {
  5. EditText editBox = (EditText) findViewById(R.id.edit_Text);
  6. String items = editBox.getText().toString();
  7. if (i == 0) {
  8. Toast.makeText(getApplicationContext(), items, Toast.LENGTH_LONG).show();
  9. i++;
  10. editBox.setText("");
  11. } else if (i == 1) {
  12. Toast.makeText(getApplicationContext(), items, Toast.LENGTH_LONG).show();
  13. i++;
  14. editBox.setText("");
  15. } else if (i == 2) {
  16. Toast.makeText(getApplicationContext(), items, Toast.LENGTH_LONG).show();
  17. i = 0;
  18. }
  19. }
  20. });

当值在单击按钮后重置时,只需在editText中输入一个值。

英文:

I think you can go for an approach like this.

  1. int clicks = 0;
  2. Button checkButton = (Button) findViewById(R.id.check_Button);
  3. checkButton.setOnClickListener(new View.OnClickListener() {
  4. public void onClick(View v) {
  5. EditText editBox = (EditText) findViewById(R.id.edit_Text);
  6. String items = editBox.getText().toString();
  7. if (i == 0) {
  8. Toast.makeText(getApplicationContext(), items , Toast.LENGTH_LONG).show();
  9. i++;
  10. editBox.setText("");
  11. } else if (i == 1) {
  12. Toast.makeText(getApplicationContext(), items , Toast.LENGTH_LONG).show();
  13. i++;
  14. editBox.setText("");
  15. } else if (i==2) {
  16. Toast.makeText(getApplicationContext(), items , Toast.LENGTH_LONG).show();
  17. i = 0;
  18. }
  19. }
  20. }

Just enter a value in the editText when the value is reset after a single button click.

答案2

得分: 0

  1. int position = 0;
  2. EditText editText = findViewById(R.id.editText);
  3. editText.addTextChangedListener(new TextWatcher() {
  4. public void afterTextChanged(Editable editable) {
  5. }
  6. public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
  7. }
  8. public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
  9. if(charSequence.length()<=0)
  10. {
  11. position = -1;
  12. }
  13. else{
  14. position = 0;
  15. }
  16. }
  17. });
  18. Button button = findViewById(R.id.button);
  19. button.setOnClickListener(new View.OnClickListener(){
  20. public void onClick(View v){
  21. int length = editText.getText().length();
  22. if(position == -1){
  23. Toast.makeText(getApplicationContext(),"Nothing in EditText box",Toast.LENGTH_LONG).show();
  24. return;
  25. }
  26. if(position == length){
  27. position = 0;
  28. }
  29. Toast.makeText(getApplicationContext(),String.valueOf(editText.getText().charAt(position)),Toast.LENGTH_LONG).show();
  30. position++;
  31. }
  32. });
英文:
  1. int position=0;
  2. EditText editText=findViewById(R.id.editText);
  3. editText.addTextChangedListener(new TextWatcher() {
  4. public void afterTextChanged(Editable editable) {
  5. }
  6. public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
  7. }
  8. public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
  9. if(charSequence.length()&lt;=0)
  10. {
  11. position=-1;
  12. }
  13. else{
  14. position=0;
  15. }
  16. }
  17. });
  18. Button button=findViewById(R.id.button);
  19. button.setOnClickListener(new View.OnClickListener(){
  20. public void onClick(View v){
  21. int length=editText.getText().length();
  22. if(position==-1){
  23. Toast.makeText(getApplicationContext(),&quot;Nothing in EditText box&quot;,Toast.LENGTH_LONG).show();
  24. return;
  25. }
  26. if(position==length){
  27. position=0;
  28. }
  29. Toast.makeText(getApplicationContext(),editText.getText().charAt(position),Toast.LENGTH_LONG).show();
  30. position++;
  31. }
  32. });

huangapple
  • 本文由 发表于 2020年8月28日 13:48:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63628139.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定