java - 如何在一个textView中显示多个变量

标签 java android

我正在 Android Studio 中开发我的第一个项目,但我陷入了“如何在一个 textView 中显示不同变量”的困境。更清楚地说,我正在开发一个需要用户输入性别的应用程序。每个性别都有自己的计算方法。所以我想在主界面和一个 TextView 中显示用户所做的计算结果。我尝试了很多次,但结果都是“0.0”

我添加了另一个 TextView ,并将每种计算方法分配给一个 TextView ,我能够显示两个结果。

public class Main_Interface extends AppCompatActivity implements View.OnClickListener{
    private TextView results;
    //private TextView fResults;//this is the second textview that I created.







    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main__interface);




        results = (TextView)findViewById(R.id.results);
        if(results.getText().toString().equals("0.0")) {
            results.setVisibility(View.INVISIBLE);
        }

        //fResults = (TextView)findViewById(R.id.fResults);
       // if(fResults.getText().toString().equals("0.0")){
        //    fResults.setVisibility(View.INVISIBLE );
       // }



        calculateMale();
        calculateFemale();
    }


    public void calculateMale(){
        SharedPreferences s = getSharedPreferences("data", Context.MODE_PRIVATE);
        double weight = s.getFloat("weight",0.0f);
        double height = s.getFloat("height",0.0f);
        int age = s.getInt("theDate", 0);


        double results2 = 66+(13.7*weight)+(5*height)-(6.8*age);
        results.setText(""+results2);



    public void calculateFemale(){
        SharedPreferences s1 = getSharedPreferences("data", Context.MODE_PRIVATE);
        double fWeight = s1.getFloat("fWeight",0.0f);
        double fHeight = s1.getFloat("fHeight",0.0f);
        int Fage = s1.getInt("theDate", 0);
        double results3 = 655 + (9.6 * fWeight) + (1.8 * fHeight) - (4.7 *Fage)
                ;
        fResults.setText(""+results3);
        SharedPreferences.Editor editor = s1.edit();
        editor.putFloat("results", (float) results3);
        editor.commit();
    }

}

在一个 TextView 中显示计算结果。

最佳答案

您在检查这两个方法之前调用这两个方法

计算男性(); 计算女性();

in onCreate() {

//do this

        calculateMale();
        calculateFemale();

    //Then check the result to make results visible or invisible.

    results = (TextView)findViewById(R.id.results);
            if(results.getText().toString().equals("0.0")) {
                results.setVisibility(View.INVISIBLE);
            }

}

关于java - 如何在一个textView中显示多个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55385300/

相关文章:

java - 使用 livedata 的 Recyclerview 在数据更改后不会更新。仅在我旋转屏幕后

java - 如何使用IDE在storm生产集群中提交拓扑

java - 如何将使用 'this' 的语句声明为静态?

android - 使用热敏打印机进行 Google 云打印

android - View 寻呼机中的 recyclerview 不会加载

java - Glassfish 3.01 上的 JSP UTF-8 字符编码错误

Java将光标更改为图像

java - 在 App Engine 上使用 Spring AOP 导致 StackOverflowError

java - 重新编译jodatime?

ProgressDialog 显示在屏幕上时的 Android 多任务处理