2012-03-12 3 views
-2

내 프로젝트에서. 세 파일이 있습니다.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

또한 공유 환경 설정을 사용하여 시도하는 저장 할 수 있습니다 값.

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"); 
관련 문제