[ASK] Parsing EditText Value on new Activity


Last Updated: 2012-07-18 15:10:03
  1. chikatz

    chikatz New Member

    Hi guys,i've just start learning android development and i've some problems with my android program. i'll be glad if there's someone who could help :D

    here's d code,
    at src/com.android.tes/ :

    • TesActivity.java
      Code (Text):
      1.  
      2. package com.android.tes;
      3.  
      4. import android.app.Activity;
      5. import android.content.Intent;
      6. import android.os.Bundle;
      7. import android.view.View;
      8. import android.view.View.OnClickListener;
      9. import android.widget.Button;
      10.  
      11. public class TesActivity extends Activity implements OnClickListener{
      12.     /** Called when the activity is first created. */
      13.     @Override
      14.     public void onCreate(Bundle savedInstanceState) {
      15.         super.onCreate(savedInstanceState);
      16.         setContentView(R.layout.main);
      17.        
      18.         Button button1 = (Button) findViewById(R.id.button1);
      19.         button1.setOnClickListener(this);
      20.     }
      21.     public void onClick(View view){
      22.         Intent intent = new Intent(this, TesActivity2.class);
      23.         startActivity(intent);
      24.     }
      25. }
      26.  
    • TesActivity2.java
      Code (Text):
      1.  
      2. package com.android.tes;
      3.  
      4. import android.app.Activity;
      5. import android.os.Bundle;
      6. import android.widget.EditText;
      7. import android.widget.TextView;
      8.  
      9. public class TesActivity2 extends Activity{
      10.     @Override
      11.     public void onCreate(Bundle savedInstanceState){
      12.         super.onCreate(savedInstanceState);
      13.         setContentView(R.layout.secondlayout);
      14.        
      15.         TextView teks = (TextView) findViewById(R.id.teks);
      16.         EditText edit1 = (EditText) findViewById(R.id.edit1);
      17.        
      18.         int val = Integer.parseInt(edit1.getText().toString());
      19.         teks.setText(Integer.toString(val));        
      20.     }
      21. }
      22.  

    yang di res/layout/ :

    • main.xml
      Code (Text):
      1.  
      2. <?xml version="1.0" encoding="utf-8"?>
      3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      4.     android:layout_width="fill_parent"
      5.     android:layout_height="fill_parent"
      6.     android:orientation="vertical" >
      7.  
      8.     <EditText
      9.         android:id="@+id/edit1"
      10.         android:layout_width="match_parent"
      11.         android:layout_height="wrap_content" >
      12.     </EditText>
      13.     <Button
      14.         android:id="@+id/button1"
      15.         android:layout_width="match_parent"
      16.         android:layout_height="wrap_content"
      17.         android:text="Button" />
      18.  
      19. </LinearLayout>
      20.  
    • secondlayout.xml
      Code (Text):
      1.  
      2. <?xml version="1.0" encoding="utf-8"?>
      3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      4.     android:layout_width="match_parent"
      5.     android:layout_height="match_parent"
      6.     android:orientation="vertical" >
      7.  
      8.     <TextView
      9.         android:id="@+id/teks"
      10.         android:layout_width="wrap_content"
      11.         android:layout_height="wrap_content"
      12.         android:text="@string/hello2"
      13.         android:textAppearance="?android:attr/textAppearanceLarge" />
      14.  
      15. </LinearLayout>
      16.  

    yang di res/values/ :

    • strings.xml
      Code (Text):
      1.  
      2. <?xml version="1.0" encoding="utf-8"?>
      3. <resources>
      4.  
      5.     <string name="hello">Hello World, TesActivity!</string>
      6.     <string name="hello2">Hello World, TesActivity2!</string>
      7.     <string name="app_name">Tes</string>
      8.  
      9. </resources>
      10.  

    AndroidManifest.xml
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    4.     package="com.android.tes"
    5.     android:versionCode="1"
    6.     android:versionName="1.0" >
    7.  
    8.     <uses-sdk android:minSdkVersion="8" />
    9.  
    10.     <application
    11.         android:icon="@drawable/ic_launcher"
    12.         android:label="@string/app_name" >
    13.         <activity
    14.             android:name=".TesActivity"
    15.             android:label="@string/app_name" >
    16.             <intent-filter>
    17.                 <action android:name="android.intent.action.MAIN" />
    18.  
    19.                 <category android:name="android.intent.category.LAUNCHER" />
    20.             </intent-filter>
    21.         </activity>
    22.         <activity
    23.             android:name=".TesActivity2"
    24.             android:label="@string/app_name" >
    25.         </activity>
    26.     </application>
    27.  
    28. </manifest>
    29.  
    the program works if the result and the source are at the same layout, but i don't know how to make it works with different activity.
    Thanks

    Advertisement
  2. darkwispr

    darkwispr Well-Known Member

    Android Training

    Thats a tutorial in which a text is passed to another activity and edited to be bigger and bold and anything you want.

    Good Luck! [Link is OK]
    I think this thread as nothing to do with app inventor?

    I think it needs a move. [Android development]

Share This Page