好的,我已经广泛搜索了这样做的方法,在我看来它似乎很简单,但当我来为我的想法编写代码时,我只是画了一个空白。
我有两个编辑文本字段,只有数字输入。我希望从另一个editText视图中减去其中一个的数字,并将其放入普通的文本视图中。
我希望在两个EditText视图都填充数字后立即更新TextView。
有没有人知道如何实现这一目标?
public class MainActivity extends Activity {
int iEditWages;
int iEditMoneySpent;
String sEditWages;
String sEditMoneySpent;
String tTotalLeftToSpend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText EditWages = (EditText) findViewById(R.id.TotalWagesNumber);
EditText EditMoneySpent = (EditText) findViewById(R.id.TotalSpentNumber);
TextView TotalLeft = (TextView) findViewById(R.id.TotalViewNumber);
sEditWages = EditWages.getText().toString();
sEditMoneySpent = EditMoneySpent.getText().toString();
iEditWages = Integer.parseInt(sEditWages);
iEditMoneySpent = Integer.parseInt(sEditMoneySpent);
int Subtract = iEditWages - iEditMoneySpent;
tTotalLeftToSpend = String.valueOf(Subtract);
TotalLeft.setText(tTotalLeftToSpend);
}
答案 0 :(得分:0)
假设您的xml布局中有两个edittext,如editText1,editText2
EditText editText1=(EditText)findViewById(R.id.editText1);
EditText editText2=(EditText)findViewById(R.id.editText2);
然后你需要获取String中的值然后转换为Integer
String s1=editText1.getText.toString();
String s2=editText1.getText.toString();
确保 NumberFormatException 此处
int i1=Integer.parseInt(s1);
int i2=Integer.parseInt(s2);
int subtract=i1-i2;
将整数转换为字符串以在TextView中打印它
String s=String.valueof(subtract);
Textview1.setText(s);
答案 1 :(得分:0)
我认为你需要使用TextWatcherListener
:
private class Watcher implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable editable) {
String str = String.valueOf(editable);
EditText edit2 = (EditText) findViewById(R.id.edit2);
String edit2Text = edit2.getText();
if(!"".equals(str) && !"".equals(edit2Text)){
int result = Integer.parseInt(str) - Inetegr.parseInt(edit2Text);
TextView tv =(TextView)findViewById(R.id.txt);
tv.setText(String.valueOf(result));
}
}
EditText edit1 = (EditText) findViewById(R.id.edit1);
edit1.addTextChandegListener(new Watcher());
你应该写这样的东西。 。
每次文本在一个editText
中更改时,它会自动获得第二个editText
- s文本,进行一些计算并将其保存在TextView
中。
答案 2 :(得分:0)
You can use text watcher on both of your edit text boxes if no order is defined( user can fill any edit text box first) otherwise use text watcher on second edit text box.
Here is sample. Hope it will help you
EditText editText1=(EditText)findViewById(R.id.editText1);
EditText editText2=(EditText)findViewById(R.id.editText2);
TextView tv=(TextView)findViewByID(R.id.textView1);
editText1.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after)
{
}
@Override
public void afterTextChanged(Editable s)
{
if(editText2.getText().toString().trim().length>0)
{
int a= Integer.parseInt(editText1.getText().toString().trim());
int b= Integer.parseInt(editText2.getText().toString().trim());
tv.settext("REsult "+a-b);
}
}
});
希望它能解决您的问题。 :)
答案 3 :(得分:0)
用此
代替您的代码 package com.app.helpproject;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
int iEditWages;
int iEditMoneySpent;
String sEditWages;
String sEditMoneySpent;
String tTotalLeftToSpend;
EditText EditWages;
EditText EditMoneySpent;
TextView TotalLeft;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditWages = (EditText) findViewById(R.id.editText1);
EditMoneySpent = (EditText) findViewById(R.id.editText2);
TotalLeft = (TextView) findViewById(R.id.textView1);
EditMoneySpent.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after)
{
if (EditWages.getText().toString().trim().length()== 0)
{
Toast.makeText(MainActivity.this, "Enter Wages First",Toast.LENGTH_LONG).show();
}
}
@Override
public void afterTextChanged(Editable s)
{
if (EditWages.getText().toString().trim().length()!= 0 && EditMoneySpent.getText().toString().trim().length() > 0)
{
int a = Integer.parseInt(EditWages.getText().toString().trim());
int b = Integer.parseInt(EditMoneySpent.getText().toString().trim());
int c=a-b;
TotalLeft.setText("Result " + c);
}
}
});
}
}
答案 4 :(得分:0)
我发现了如何让它发挥作用。它是用户留下的Text观察者示例的组合,也是将字符串转换为整数并以Nambi为我提供的方式减去它们的组合。这是我的最终和工作代码! :)
public class MainActivity extends Activity implements TextWatcher {
int iEditWages;
int iEditMoneySpent;
int iSubtract;
String sEditWages;
String sEditMoneySpent;
String sTotalLeftToSpend;
EditText EditWages;
EditText EditMoneySpent;
TextView TotalLeft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditMoneySpent = (EditText) findViewById(R.id.TotalSpentNumber);
EditWages = (EditText) findViewById(R.id.TotalWagesNumber);
EditMoneySpent.addTextChangedListener(this);
EditWages.addTextChangedListener(this);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
EditWages = (EditText) findViewById(R.id.TotalWagesNumber);
EditMoneySpent = (EditText) findViewById(R.id.TotalSpentNumber);
TotalLeft = (TextView) findViewById(R.id.TotalViewNumber);
sEditWages = EditWages.getText().toString();
sEditMoneySpent = EditMoneySpent.getText().toString();
try {
iEditWages = Integer.parseInt(sEditWages);
iEditMoneySpent = Integer.parseInt(sEditMoneySpent);
iSubtract = iEditWages - iEditMoneySpent;
} catch (NumberFormatException e) {
}
sTotalLeftToSpend = String.valueOf(iSubtract);
TotalLeft.setText(sTotalLeftToSpend);
}