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

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

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:

Button checkButton = (Button) findViewById(R.id.check_Button);
checkButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        
        EditText editBox = (EditText) findViewById(R.id.edit_Text);
        String items = editBox.getText().toString();
        Scanner scanner = new Scanner(items);
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            Toast.makeText(getApplicationContext(), line, Toast.LENGTH_SHORT).show();
        }
        scanner.close();
        
    }
}
);

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

翻译:

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

像这样:

单击按钮一次:

提示不断显示:

A

B

C

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

像这样:

EditText 值:
A

B

C

第一次点击结果:

仅 A

第二次点击结果:

仅 B

第三次点击结果:

仅 C

就像这样

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

Button checkButton = (Button) findViewById(R.id.check_Button);
checkButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        
        EditText editBox = (EditText) findViewById(R.id.edit_Text);
        String items = editBox.getText().toString();
        Scanner scanner = new Scanner(items);
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            Toast.makeText(getApplicationContext(), line, Toast.LENGTH_SHORT).show();
        }
        scanner.close();
        
    }
}
);

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

英文:

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:

Button checkButton = (Button) findViewById(R.id.check_Button);
		checkButton.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					
					EditText editBox = (EditText) findViewById(R.id.edit_Text);
					String items = editBox.getText().toString();
					Scanner scanner = new Scanner(items);
					while (scanner.hasNextLine()) {
						String line = scanner.nextLine();
						Toast.makeText(getApplicationContext(), line, Toast.LENGTH_SHORT).show();
					}
					scanner.close();
					
				}
			}
		);

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

答案1

得分: 0

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

int clicks = 0;
Button checkButton = (Button) findViewById(R.id.check_Button);
checkButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        EditText editBox = (EditText) findViewById(R.id.edit_Text);
        String items = editBox.getText().toString();
        if (i == 0) {
            Toast.makeText(getApplicationContext(), items, Toast.LENGTH_LONG).show();
            i++;
            editBox.setText("");
        } else if (i == 1) {
            Toast.makeText(getApplicationContext(), items, Toast.LENGTH_LONG).show();
            i++;
            editBox.setText("");
        } else if (i == 2) {
            Toast.makeText(getApplicationContext(), items, Toast.LENGTH_LONG).show();
            i = 0;
        }
    }
});

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

英文:

I think you can go for an approach like this.

    int clicks = 0;
    Button checkButton = (Button) findViewById(R.id.check_Button);
    checkButton.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
         EditText editBox = (EditText) findViewById(R.id.edit_Text);
         String items = editBox.getText().toString();
         if (i == 0) {  
            Toast.makeText(getApplicationContext(), items , Toast.LENGTH_LONG).show();  
            i++;  
            editBox.setText("");
         } else if (i == 1) {  
           Toast.makeText(getApplicationContext(), items , Toast.LENGTH_LONG).show();  
           i++; 
           editBox.setText("");
         } else if (i==2) {
           Toast.makeText(getApplicationContext(), items , Toast.LENGTH_LONG).show();  
           i = 0;
         }  
  } 
    
}

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

答案2

得分: 0

int position = 0;
EditText editText = findViewById(R.id.editText);
editText.addTextChangedListener(new TextWatcher() {
    public void afterTextChanged(Editable editable) {
        
       
    }

    public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
    }

    public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
       if(charSequence.length()<=0)
	   {
			position = -1;
	   }
	   else{
			position = 0;
	   }
       
    }
});
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){
        int length = editText.getText().length();
        if(position == -1){
            Toast.makeText(getApplicationContext(),"Nothing in EditText box",Toast.LENGTH_LONG).show();
            return;
        }
        if(position == length){
            position = 0;    
        }
        
        Toast.makeText(getApplicationContext(),String.valueOf(editText.getText().charAt(position)),Toast.LENGTH_LONG).show();
        position++;
        
    }
});
英文:
int position=0;
EditText editText=findViewById(R.id.editText);
editText.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable editable) {
            
           
        }

        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
           if(charSequence.length()&lt;=0)
		   {
				position=-1;
		   }
		   else{
				position=0;
		   }
		   
        }
    });
Button button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
	public void onClick(View v){
		int length=editText.getText().length();
		if(position==-1){
			Toast.makeText(getApplicationContext(),&quot;Nothing in EditText box&quot;,Toast.LENGTH_LONG).show();
			return;
		}
		if(position==length){
			position=0;	
		}
		
			Toast.makeText(getApplicationContext(),editText.getText().charAt(position),Toast.LENGTH_LONG).show();
			position++;
		
	}
});

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:

确定