在一个TextView中的总值,并把总数在另一个textview为android

在一个TextView中的总值,并把总数在另一个textview为android

问题描述:

我在一个textView中有一些值。例如:在一个TextView中的总值,并把总数在另一个textview为android

200 
300 
100 
500 

如何合计这些值并将其放入另一个textview中?当我打开应用程序时,它停止了。帮助me..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> 

顺便说一句,我还是新来android.:D

你可以从你的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 
+0

我用你的代码和更新我的文章。这样做是正确的吗? – Logitech 2015-02-10 03:44:37