2017-08-12 49 views
-1

將一個活動的添加結果傳遞給另一個活動我創建了一個名爲SplashActivity的類。我在此活動中添加了兩個數字。我想在下一個活動中顯示答案,但它不顯示在那裏。我創建了兩個EditText字段,然後是一個按鈕。當按鈕被點擊時,新的活動應該開始並且應該顯示該添加的答案。如何通過android

EditText e=(EditText)findViewById(R.id.editText3); 
    EditText e2=(EditText)findViewById(R.id.editText4);` 
    int num1=Integer.parseInt(e.getText().toString()); 
    int num2=Integer.parseInt(e2.getText().toString()); 
    int sum=num1+num2; 
    Intent in=new Intent(this,SecondSplash.class); 
    in.putExtra("value",sum); 
    startActivity(in); 

及其XML是

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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.sammar.firstapplication.SplashActivity" 
    android:background="@drawable/background"> 

    <EditText 
     android:id="@+id/editText3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="76dp" 
     android:ems="10" 
     android:inputType="numberSigned" 
     android:hint="Enter First Number"/> 

    <EditText 
     android:id="@+id/editText4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="24dp" 
     android:ems="10" 
     android:inputType="numberSigned" 
     android:hint="Enter Second Number" 
     android:layout_below="@+id/editText3" 
     android:layout_alignStart="@+id/editText3" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginStart="46dp" 
     android:onClick="onButtonClick" 
     android:text="ADD" 
     android:layout_above="@+id/button6" 
     android:layout_alignParentStart="true" 
     android:layout_marginBottom="51dp" /> 

這是SecondActivity的代碼。在這個活動中,我創建了一個接收數據的包。

TextView txtView; 
    String value; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_second_splash); 

     Bundle b=getIntent().getExtras(); 
     if(b!=null) 
     { 
      value=b.getString("value"); 
     } 
     txtView =(TextView)findViewById(R.id.textView2); 
     //value=getIntent().getExtras().getString("value"); 
     txtView.setText(value); 

和XML是

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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.sammar.firstapplication.SecondSplash"> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginStart="132dp" 
     android:layout_marginTop="191dp" 
     /> 
</RelativeLayout> 

我不能讓第二活性和的值。請幫助enter code here

回答

0
value=b.getInt("value"); 

類型總和的是INT和不字符串

+0

我是否還需要在fristactivity解析總和爲String? –

+0

txtView.setText(value);需要一個字符串 –

0

把裏面的onClick喜歡你的意圖,

button.setOnClickListner(new OnClickListner) 
{ 
    @override OnClick() 
    { 
     Intent in=new Intent(this,SecondSplash.class); 
     in.putExtra("value",String.valueOf(sum)); 
     startActivity(in); 
    } 
} 

更改代碼

Bundle b=getIntent().getExtras(); 
    if(b!=null) 
    { 
     value=b.getString("value"); 
    } 

String value = getIntent().getStringExtra("value"); 
+0

我改變了代碼,但它仍然沒有工作 –

+0

檢查我編輯的ans ... in.putExtra()行 –