java - 如何将 EditText 中的多个值设置为 1 Textview 并自动显示而无需单击按钮

标签 java android eclipse

是否可以将多个EditText设置为1个Textview并自动显示而无需单击任何按钮。这是 XML 示例。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/labelnim"
            android:layout_marginTop="7dp"
            android:text="Name :"
            android:textSize="18sp" />


        <EditText
            android:id="@+id/name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="text" >

        </EditText>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/labelnama"
            android:layout_marginTop="25dp"
            android:text="Spesification"
            android:textSize="18sp" />


        <EditText
            android:id="@+id/spesification"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:inputType="text"
            >

        </EditText>

           <TextView
           android:id="@+id/TextView Output"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/labelnama"
            android:layout_marginTop="25dp"
            android:text="Output"
            android:textSize="18sp" />



</LinearLayout>

我希望数据在 TextViewOutput 中显示为名称 + 规范。另外,当我输入时,输出会自动更改为每当我输入 Both EditText 时

最佳答案

是的,您必须使用addTextChangedListener按照以下代码操作

editText.addTextChangedListener(new 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) {
  String editValue = editText.getText().toString();
  if(editValue.lenght!=0){
  String textValue = yourTextView.getText().toString();
  int val = Integer.parse(textValue);
  int edVal = Integer.parse(editValue);

  int result = edVal * val;

  resultText.setText(result);

  }

}

@Override
public void afterTextChanged(Editable s) {
}
});

希望这对您有帮助。

关于java - 如何将 EditText 中的多个值设置为 1 Textview 并自动显示而无需单击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56930466/

相关文章:

Java JDBC 结果集错误,但为什么呢?

java - Apache Spark - 添加两列

java ee sdk下载找不到jdk

android - Eclipse 错误 - 我的 Android 项目中的错误没有真正的错误

java - 元素类型 “struts-config” 的内容必须匹配“(datasource?,form-b​​eans?

java - 如何阻止 Eclipse 格式化程序将所有枚举放在一行上

java - 如何编写代码来计算数组中特定项的出现次数?

java - 从 JSON 解析嵌套值并添加到 HashMap

android - 更新 Google Play 服务后应用程序才会运行

android - 如何为通用 android fragment 创 build 计思维