【发布时间】:2020-03-31 05:26:04
【问题描述】:
我目前有一个摄氏温度的 EditText 和华氏温度的 TextView 来显示答案。我使用了 TextChangedListener 但这不适用于 EditText。我无法使用 TextChangedListener 更改 EditText 中的值。有没有办法我可以做到这一点。?我希望用户在任何 EditTexts 中输入一个值并在另一个中获得结果。这是我的 TextView 代码。我的主要活动:
public class MainActivity extends ActionBarActivity {
EditText C, F;
TextView Ct, Ft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
C = (EditText) findViewById(R.id.celsius);
F = (EditText) findViewById(R.id.fahrenheit);
Ct = (TextView) findViewById(R.id.celsiusTextView);
Ft = (TextView) findViewById(R.id.fahrenheitTextView);
C.addTextChangedListener(new TextWatcher() {
double f,
c;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().equals("")) {
c = Double.parseDouble(s.toString());
f = (((9 / 5) * c) + 32);
Ft.setText(String.valueOf(f));
} else {
Ft.setText("");
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
F.addTextChangedListener(new TextWatcher() {
double f,
c;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().equals("")) {
f = Double.parseDouble(s.toString());
c = ((f - 32) * 5 / 9);
Ct.setText(String.valueOf(c));
} else {
Ct.setText("");
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
还有我的 XML:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/nameText"
android:background="@drawable/fab_linear_layout">
<TextView
android:text="@string/converter"
android:layout_gravity="bottom"
android:textSize="30sp"
android:textColor="#FFFFFF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/nameText"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:id="@+id/linearLayout1"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<EditText
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberSigned|numberDecimal"
android:hint="@string/celsius"
android:id="@+id/celsius"/>
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/fahrenheit"
android:id="@+id/fahrenheitTextView"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/linearLayout1"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:id="@+id/linearLayout2"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<EditText
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberSigned|numberDecimal"
android:hint="@string/fahrenheit"
android:id="@+id/fahrenheit"/>
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/celsius"
android:id="@+id/celsiusTextView"/>
</LinearLayout>
从代码中可以看出,我想去掉多余的TextViews,而是在EditText中得到答案。
【问题讨论】:
-
把你的代码放在 afterTextChanged 方法上
-
但它仍然不适用于 EditTexts。 TextChangedListener 仅适用于 TextViews。
-
所以你要找的是只有 2 个编辑文本,对吗?并删除文本视图?以 2 个项目的布局结束。
-
正是如此。实际上,我还将添加 Kelvin 选项。并且每个 EditTexts 都有 2 个 TextView 是不合理的。
-
你为什么说不能使用edittext?
标签: android responsive-design android-edittext textview