通过单击按钮将edittext的值添加到textview

时间:2017-05-09 15:02:39

标签: android

我目前正在开展一个小项目。其中包含textview,按钮和edittext。 textview设置为1,当单击按钮时,textview上升1.我可以用任何数字填充edittext(例如包括-1)。我需要从edittext获取值,并通过单击相同的按钮将其添加到textview中的当前值。我该怎么做呢?这听起来很容易但我似乎无法找到解决方案。

MainActivity

package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    int numberText = 1;
    EditText  editTextID;

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

        final TextView textViewID = (TextView) findViewById(R.id.textViewID);
        textViewID.setText("1");
    }
    public void changeValue(View view){
        numberText+=1;
        TextView textViewID = (TextView) findViewById(R.id.textViewID);
        textViewID.setText(numberText+"");

    }
}

Activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.myapplication.MainActivity">

    <TextView
        android:id="@+id/textViewID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.238" />

    <Button
        android:id="@+id/PlusID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="changeValue"
        android:text="@string/plus"
        tools:ignore="MissingConstraints"
        android:layout_marginTop="50dp"
        app:layout_constraintTop_toBottomOf="@+id/textViewID"
        android:layout_marginLeft="0dp"
        app:layout_constraintLeft_toLeftOf="@+id/textViewID"
        android:layout_marginRight="0dp"
        app:layout_constraintRight_toRightOf="@+id/textViewID" />

    <EditText
        android:id="@+id/editTextID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintTop_toBottomOf="@+id/PlusID"
        android:layout_marginLeft="0dp"
        app:layout_constraintLeft_toLeftOf="@+id/PlusID"
        android:layout_marginRight="0dp"
        app:layout_constraintRight_toRightOf="@+id/PlusID" />

</android.support.constraint.ConstraintLayout>

4 个答案:

答案 0 :(得分:0)

您可以在changeValue中获取EditText值,如下所示:

EditText editTextID = (EditText ) findViewById(R.id.editTextID);
try {
    int editValue = Integer.parseInt(editTextID.getText().toString());
} catch (NumberFormatException e) {
    // edit text value not a valid integer
}

然后将editValue添加到numberText并在TextView中显示应该很简单。

答案 1 :(得分:0)

int currentNum=Integer.parseInt(textView.getText().toString());
        int addedNum=Integer.valueOf(editText.getText().toString());
        currentNum+=addedNum;
        textView.setText(String.valueOf(currentNum));

答案 2 :(得分:0)

以下是:

public void changeValue(View view) {

    // Retrieve the content from EditText
    editTextID = (EditText) findViewById(R.id.edit_text);
    String content = editTextID.toString();

    // Then the value of that content
    // Wrap below statement in a try-catch for a NumberFormatException
    int value1 = Integer.parseInt(content);

    // Add it to value from TextView
    TextView textViewID = (TextView) findViewById(R.id.textViewID);

    // Wrap below statement in a try-catch for a NumberFormatException
    int value2 = Integer.parseInt(textViewID.toString());

    // Finally set the correct value
    int value = value1 + value2;
    textViewID.setText(value + "");

}

此外,将对TextViewEditText的引用存储为类字段,而不是再次调用findViewById()

答案 3 :(得分:0)

首先尝试初始化所需的视图&amp;存储View实例......这将使您的任务变得更加容易。

 public class MainActivity extends AppCompatActivity {

        private TextView displayValueTextView;
        private EditText valueEditText;
        private Button button;

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

            initViews();
        }

        private void initViews(){
            displayValueTextView = (TextView) findViewById(R.id.tv_value);
            valueEditText = (EditText) findViewById(R.id.et_input);
            button = (Button) findViewById(R.id.button);
        }
    }

这是XML布局,因此您不会对视图及其关联的Id感到困惑;

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/rl_activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.android.myapplication.MainActivity">


        <TextView
            android:id="@+id/tv_value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="26sp"
            android:layout_centerInParent="true"
            android:maxLines="1"
            android:text="Hello World"/>

        <EditText
            android:id="@+id/et_input"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:ems="9"
            android:hint="@string/hint_text"
            android:maxLines="1"/>

        <Button
            android:id="@+id/button"
            android:text="@string/plus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="changeValue"
            android:layout_toRightOf="@+id/et_input"
            android:layout_alignBottom="@+id/et_input"/>


    </RelativeLayout>

Button的onClick属性设置为changeValue,这是我们的方法,它将改变TextView的值,具体取决于EditText上的数字。该方法如下所示:

public void changeValue(View view) {
    double numberToAdd;
    double currentValue;

    try {
        numberToAdd = Double.parseDouble(valueEditText.getText().toString());
    } catch (Exception ex) {
        ex.printStackTrace();
        Toast.makeText(this, "Edit Text must contain a number.", Toast.LENGTH_LONG).show();
        valueEditText.setText("");
        numberToAdd = 0;
    }
    try {
        currentValue = Double.parseDouble(displayValueTextView.getText().toString());
    } catch (Exception ex) {
        ex.printStackTrace();
        currentValue = 0;
        Toast.makeText(this, "Current value is not a number. It is now set to 0.", Toast.LENGTH_LONG).show();
        displayValueTextView.setText("0");
    }

    double finalValue = currentValue + numberToAdd;
    displayValueTextView.setText(finalValue + "");

}

numberToAdd变量是我们添加到当前TextView的数字......我们必须;

  1. 通过获取EditText的文本并将其解析为double(这将允许我们处理EditText中的小数位数)来提取EditText所拥有的值。

  2. 然后我们提取TextView的当前值(如果它不是一个数字,将抛出异常,textview将默认为0)。

  3. 最后,我们添加两个值,并将TextView设置为表示添加值的字符串。