在一个TextView中的总值,并将总值放在另一个android文本视图中



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

200
300
100
500

如何将这些值相加并放到另一个文本视图中?当我打开应用程序时,它停止了。帮帮我…3天了,我被困在这里

这是代码:

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>

顺便说一句,我还是安卓的新手D

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

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

最新更新