我正在android studio中制作一个长度转换程序,其中有2个微调器和2个EditText。我使用EditText小部件让用户输入任何值,当我使用USB调试器在设备上运行代码时,当我尝试输入值时,应用卡住,并且崩溃了一段时间。请提供任何有关如何使其正常工作的建议,以帮助我。 以下是我尝试过的代码参考:
XML部分:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:weightSum="2">
<com.jaredrummler.materialspinner.MaterialSpinner
android:id="@+id/spinnerconverter1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dp"
android:textAlignment="center"
android:textStyle="normal"
app:ms_background_color="#E3E0E3E3" />
</LinearLayout>
<EditText
android:id="@+id/inputlength1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/btn_gv_round"
android:hint="0"
android:gravity="center"
android:padding="10dp"
android:inputType="numberDecimal"
android:textAlignment="center"
android:textSize="30sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:weightSum="2">
<com.jaredrummler.materialspinner.MaterialSpinner
android:id="@+id/spinnerconverter2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dp"
android:textAlignment="center"
app:ms_background_color="#E3E0E3E3" />
</LinearLayout>
<EditText
android:id="@+id/inputlength2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/btn_gv_round"
android:hint="@string/_0"
android:gravity="center"
android:padding="10dp"
android:textAlignment="center"
android:inputType="numberDecimal"
android:textSize="30sp"
/>
Java部分:
public class LengthConverterFragment extends Fragment {
private EditText inputlength1, inputlength2;
private MaterialSpinner spinnerconverter1, spinnerconverter2;
@SuppressLint("ClickableViewAccessibility")
public View onCreateView(@NonNull final LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_length_converter, container, false);
inputlength1 = root.findViewById(R.id.inputlength1);
inputlength2 = root.findViewById(R.id.inputlength2);
imghand = root.findViewById(R.id.imghand);
spinnerconverter1 = root.findViewById(R.id.spinnerconverter1);
spinnerconverter2 = root.findViewById(R.id.spinnerconverter2);
spinnerconverter1.setItems("Nanometres", "Micrometres", "Millimetres", "Centimetres", "Metres", "Kilometres", "Inches", "Feets", "Yards",
"Miles", "Nautical Miles", "Lunar Distance");
spinnerconverter2.setItems("Nanometres", "Micrometres", "Millimetres", "Centimetres", "Metres", "Kilometres", "Inches", "Feets", "Yards",
"Miles", "Nautical Miles", "Lunar Distance");
inputlength1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@SuppressLint("SetTextI18n")
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String l1 = inputlength1.getText().toString();
if (l1.length() > 0) {
if (spinnerconverter1.getText().equals("Nanometres") && spinnerconverter2.getText().equals("Nanometres")) {
inputlength2.setText("" + l1);
} else if (spinnerconverter1.getText().equals("Nanometres") && spinnerconverter2.getText().equals("Micrometres")) {
String res = String.valueOf(Double.parseDouble(l1) * 0.001);
inputlength2.setText("" + res);
}
} else if (l1.length() == 0) {
inputlength2.setText("0");
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
inputlength2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@SuppressLint("SetTextI18n")
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String l2 = inputlength2.getText().toString();
if (l2.length() > 0) {
if (spinnerconverter2.getText().equals("Nanometres") && spinnerconverter1.getText().equals("Nanometres")) {
inputlength1.setText("" + l2);
} else if (spinnerconverter2.getText().equals("Nanometres") && spinnerconverter1.getText().equals("Micrometres")) {
String res = String.valueOf(Double.parseDouble(l2) * 0.001);
inputlength1.setText("" + res);
}
}else if (l2.length() == 0) {
inputlength1.setText("0");
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
return root;
}
}