我知道还有其他一些答案,我已经尝试过所有这些答案而无法让它发挥作用。我需要保存两个输出数据“brereton”和“hoppus”。我认为我的编码可以做得更好,但我对此很陌生,所以不要严厉地评判......提前得到任何帮助。
package com.example.dpivl.teak101;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Tab1 extends Fragment {
EditText num1;
EditText num2;
EditText num3;
TextView brereton;
TextView hoppus;
Button calculate;
Button reset;
double w = 0;
double x = 0;
double y = 0;
double z = 0;
double h = 0;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab_1, container, false);
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initControls();
}
private void initControls() {
num1 = (EditText) getView().findViewById(R.id.editTextHeight);
num2 = (EditText) getView().findViewById(R.id.editTextWidth);
num3 = (EditText) getView().findViewById(R.id.editTextAmount);
brereton = (TextView) getView().findViewById(R.id.answerBrereton);
hoppus = (TextView) getView().findViewById(R.id.answerHoppus);
calculate = (Button) getView().findViewById(R.id.buttonCalc);
reset = (Button) getView().findViewById(R.id.reset);
reset.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
reset();
}
});
calculate.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
if (num1.getText().toString().equals("")|| num2.getText().toString().equals("")|| num3.getText().toString().equals("")){
Toast.makeText(getActivity(),"Insert Measurements!",Toast.LENGTH_SHORT).show();
}
else {
calculate();
}
}
});
}
private void calculate() {
w = Double.parseDouble(num1.getText().toString());
x = Double.parseDouble(num2.getText().toString());
y = Double.parseDouble(num3.getText().toString());
z = (x / 314) * (x / 314) * w * y;
h = z * 0.7852;
brereton.setText(Double.toString(z));
hoppus.setText(Double.toString(h));
}
private void reset() {
num1.setText("");
num2.setText("");
num3.setText("");
brereton.setText("");
hoppus.setText("");
}
}
答案 0 :(得分:1)
将textviews的文本保存在这样的包中:
@Override
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
outState.putString("brereton", brereton.getText().toString());
outState.putString("hoppus", hoppus.getText().toString());
super.onSaveInstanceState(outState);
}
现在,当活动旋转时,将重新创建片段,并且UI组件将重新膨胀到视图层次结构中。初始化完成后(正如您在onActivityCreated回调中所做的那样),您可以在回调中使用该包,将文本设置为您的文本视图。
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
initControls();
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
brereton.setText(savedInstanceState.getString("brereton"));
hoppus.setText(savedInstanceState.getString("hoppus"));
}
}
希望这有帮助。