Как разделить десятичное число на два TextView в андроид студии - PullRequest
0 голосов
/ 07 октября 2018

Я работаю над этим проектом по разработке приложений для Android.Я хочу разбить десятичное число, например 2.91, на два TextViews, каждый TextView принимает часть числа, как первый TextView принимает 2, а второй TextView принимает 91 без десятичных точек

Здесьмой код:

Test.Java

public class Test extends AppCompatActivity {

Button btSpilt;
EditText Input;
TextView wholenumber, points;
private double d;
private long a, b;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    btSpilt = findViewById(R.id.Test);
    btSpilt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            double d = Input.getText();

            long a = (long) d;
            double f = d - a;

            while (Math.abs((long) f - f) > 0.000001) f *= 10;

            long b = (long) f;

            wholenumber.setText((int) a);
            points.setText((int) b);
        }
    });
}
}

Activity_test.xml

<EditText
    android:id="@+id/Input"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="input"
    android:inputType="numberDecimal"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    tools:layout_editor_absoluteY="76dp" />

<Button
    android:id="@+id/btSpilt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Split"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    tools:layout_editor_absoluteY="146dp" />

<TextView
    android:id="@+id/wholenumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Whole Number"
    android:textSize="25sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    tools:layout_editor_absoluteY="244dp" />

<TextView
    android:id="@+id/points"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Points"
    android:textSize="25sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.55" />

1 Ответ

0 голосов
/ 07 октября 2018

Добро пожаловать в StackOverflow.
Если я правильно понимаю, ваша конкретная проблема заключается в том, что вы хотите разделить ввод дважды на десятичную и целую части.
Вы можете сделать это следующим образом:
String[] number = Input.getText().toString().split(".");

Таким образом, вы можете переписать свой метод следующим образом:

public void onClick(View v) {

    String[] number = Input.getText().toString().split(".");

    wholenumber.setText(number[0]);
    points.setText(number[1]);
}

Однако вы заметите, что number[1] может возвращать null для недесятичных чисел.Поэтому вам, возможно, придется позаботиться об этом конкретном случае.

Надеюсь, это поможет

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...