2012-03-12 11 views
-2

私のプロジェクトで。 3つのファイルがあります。Androidの問題:edittextからコンテンツを取得する方法

passParaActivity.java 
//main Activity, show a edittext and a button to reslut.java 
result.java 
//display the number which typed in passParaAcitivity.java 
mainFunc.java 
//function to get the number from passParaActivity.java 

これは、passParaActivity.javaに関連するmain.xmlです。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <TextView android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Type a number:" 
      android:textSize="20dp"/> 
    <EditText android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:hint="Type a number" 
      android:id="@+id/number"/> 
    <Button android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="PASS" 
      android:id="@+id/btn"/> 
</LinearLayout> 

これはresult.xmlのコードです。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="The number is:" 
       android:textSize="20dp"/> 
    <TextView android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="30dp" 
       android:textColor="#0000ff" 
       android:id="@+id/mytext"/> 
    <Button android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Back" 
      android:id="@+id/back"/> 

</LinearLayout> 

passParaActivity.java:

public Button btn; 
    public EditText number; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     btn = (Button) findViewById(R.id.btn); 
     number = (EditText) findViewById(R.id.number); 

     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent intent = new Intent(PassParaActivity.this, result.class); 
       startActivity(intent); 
      } 
     }); 
    } 

result.java:

public TextView mytext; 
    public Button back; 
    mainFunc main; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.result); 
     mytext = (TextView) findViewById(R.id.mytext); 
     back = (Button) findViewById(R.id.back); 

     main = new mainFunc(result.this); 
     mytext.setText(main.getNum()); 

     back.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent intent = new Intent(result.this, PassParaActivity.class); 
       startActivity(intent); 
      } 
     }); 
    } 

mainFunc.java

public class mainFunc { 
    private PassParaActivity mycontext; 
    int num = 0; 
    public mainFunc(Context context){ 
     mycontext = (PassParaActivity) context; 
    } 

    public int getNum(){ 
     num = Integer.parseInt(mycontext.number.getText().toString()); 
     return num; 
    } 
} 

私が働いて、このいずれかを取得できますか?あなたの患者に感謝します。

答えて

0

あなたが他の1つのアクティビティからのデータを渡す方法を求めている場合は、あなたがテントエキストラを使用することができますが:

intent.putExtra("Number", "Value"); 

を参照してください:How to start an Intent by passing some parameters to it?

+0

華麗それをretriveする、私は、多くのおかげでこれをしようとします! – nich

0

また、保存するために共有設定を使用して試すことができますその価値。

SharedPreferences prefs = PreferenceManager 
.getDefaultSharedPreferences(this.getApplicationContext()); 
    Editor prefsEditor = prefs.edit(); 
    prefsEditor.putString("key","value"); 
    prefsEditor.commit(); 

そして

SharedPreferences prefs = PreferenceManager 
.getDefaultSharedPreferences(this.getApplicationContext()); 
String value = prefs.getString("key","defaultValue"); 
関連する問題