TextView中的总值,并将总数放在另一个textview for android中

时间:2015-02-10 03:06:38

标签: java android textview

我在一个textView中有一些值。例如:

200
300
100
500

如何将这些值合计并将其放入另一个textview中?当我打开应用程序时,它停止了。帮助我......现在我被困在这里

这是代码:

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

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

        TextView textView = (TextView) findViewById(R.id.t1);
        TextView textView1 = (TextView) findViewById(R.id.t2);
        textView.setText(100+"\n" + 200+"\n" + 300+"\n");

        String toParse = textView.getText().toString(); //get the text from the source textview
        String [] numbers = toParse.split("\\r?\\n");
        int total = 0;
        for(String s : numbers)
        {
            total += Integer.parseInt(s);
        }
        textView1.setText(total); //set the text to the target textview
    }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

布局:

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="100 200 300 400"
        android:id="@+id/t1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/t2"
        android:layout_below="@+id/t1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="82dp" />
</RelativeLayout>
顺便说一句,我还是android.:D

的新手

1 个答案:

答案 0 :(得分:0)

您可以从textview中获取文本并通过换行符分割它们,然后将换行符存储在数组中并通过将字符串转换为整数来添加它们

String toParse = tvObject.getText().toString(); //get the text from the source textview
String [] numbers = toParse.split("\\r?\\n");
int total = 0;
for(String s : numbers)
{
    total += Integer.parseInt(s);
}
secondTvObject.setText(total); //set the text to the target textview