Android问题:如何从edittext获取内容
在我的项目中。有三个文件Android问题:如何从edittext获取内容,android,Android,在我的项目中。有三个文件 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 这是与passPa
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.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;
}
}
我怎样才能让这个工作?感谢您的患者。如果您询问如何将数据从一个活动传递到另一个活动,您可以使用Intent extras:
intent.putExtra("Number", "Value");
请参阅:您也可以尝试使用共享首选项来存储值
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");
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");