我正在尝试制作一个将两个我已经写过的数字相加并在下一个活动中显示结果的应用程序。我可以将其与非零数字一起使用,但是当我向另一个数字加0时,结果始终为零。我知道这是因为默认值0。但是它可以与其他值一起正常工作。当我加上零以外的任何数字时,它可以正常工作,但是当包含0时,整个结果将为零。请帮我。谢谢!
MainActivity
pd.DataFrame({"ColumnName": df.columns}).to_csv("path/to/file.csv")
activity_main
package com.example.david.magic;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);}
public void no (View view){
EditText editTextOne = (EditText) findViewById(R.id.textView);
EditText editTextTwo = (EditText) findViewById(R.id.textView1);
int firstNumber = Integer.parseInt(editTextOne.getText().toString());
int secondNumber = Integer.parseInt(editTextTwo.getText().toString());
int sum = firstNumber + secondNumber;
// Now send the result sum to the Activity B through intent.
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra("RESULT_SUM", sum);
startActivity(intent);
}
public void yes (View view){
EditText editTextOned = (EditText) findViewById(R.id.resultno);
EditText editTextTwod = (EditText) findViewById(R.id.textView);
int firstNumberd = Integer.parseInt(editTextOned.getText().toString());
int secondNumberd = Integer.parseInt(editTextTwod.getText().toString());
int sumd = firstNumberd + secondNumberd;
// Now send the result sum to the Activity B through intent.
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra("RESULT_SUMd", sumd);
startActivity(intent);
}}
Main2Activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.217"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.961"
android:text="Yes"
android:onClick="yes"/>
<Button
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.739"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.961"
android:text="No"
android:onClick="no"/>
<EditText
android:id="@+id/textView"
android:layout_width="182dp"
android:layout_height="58dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="1"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/textView1"
android:layout_width="174dp"
android:layout_height="49dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="0"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.381" />
<EditText
android:id="@+id/resultno"
android:layout_width="170dp"
android:layout_height="50dp"
android:layout_marginEnd="104dp"
android:layout_marginRight="104dp"
app:layout_constraintEnd_toEndOf="parent"
android:text="0"
android:visibility="invisible"
tools:layout_editor_absoluteY="128dp" />
<ImageView
android:layout_width="366dp"
android:layout_height="282dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/one"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.289" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:1)
问题来自这段代码:
EditText hi = findViewById(R.id.textView2);
Bundle extras = getIntent().getExtras();
if (extras != null) {
int sum = getIntent().getIntExtra("RESULT_SUM", 0);
hi.setText(String.valueOf(sum));
}
EditText bye = findViewById(R.id.textView2);
Bundle extra = getIntent().getExtras();
if (extras != null) {
int sumd = getIntent().getIntExtra("RESULT_SUMd", 0);
bye.setText(String.valueOf(sumd));
}
无论您单击是或否,extras
都不为空。因此,当您单击否时,您会在第一个if
中获得righ值。但是第二if
总是被触发。由于在两种情况下都为textView2调用setText,因此bye.setText(String.valueOf(sumd));
会将正确的值覆盖为0,因为“ RESULT_SUMd”为空且默认值为0。
让我在注释中解释代码(案例编号):
EditText hi = findViewById(R.id.textView2);
Bundle extras = getIntent().getExtras();
//always true
if (extras != null) {
// sum is set with right value
int sum = getIntent().getIntExtra("RESULT_SUM", 0);
//textView2 display the right sum
hi.setText(String.valueOf(sum));
}
EditText bye = findViewById(R.id.textView2);
Bundle extra = getIntent().getExtras();
//always true
if (extras != null) {
//no result_sumd found
//sumd == 0
int sumd = getIntent().getIntExtra("RESULT_SUMd", 0);
//textView2 text is override with 0, value of sumd
bye.setText(String.valueOf(sumd));
}
当然,如果单击“是”,那么一切都会正常,因为在这种情况下定义了“ RESULT_SUMd”。
到目前为止,我建议您添加另一个标志(以表示意图来自“是” /“否”),并在流量增加时更好:
在第一次活动中:
public void no (View view){
EditText editTextOne = (EditText) findViewById(R.id.textView);
EditText editTextTwo = (EditText) findViewById(R.id.textView1);
int firstNumber = Integer.parseInt(editTextOne.getText().toString());
int secondNumber = Integer.parseInt(editTextTwo.getText().toString());
int sum = firstNumber + secondNumber;
// Now send the result sum to the Activity B through intent.
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra("RESULT_SUM", sum);
//add extra flag
intent.putExtra("FROM_NO", true);
startActivity(intent);
}
public void yes (View view){
EditText editTextOned = (EditText) findViewById(R.id.resultno);
EditText editTextTwod = (EditText) findViewById(R.id.textView);
int firstNumberd = Integer.parseInt(editTextOned.getText().toString());
int secondNumberd = Integer.parseInt(editTextTwod.getText().toString());
int sumd = firstNumberd + secondNumberd;
// Now send the result sum to the Activity B through intent.
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra("RESULT_SUMd", sumd);
startActivity(intent);
}}
在第二个活动中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
// hi is enough
EditText hi = findViewById(R.id.textView2);
Bundle extras = getIntent().getExtras();
if (extras != null) {
boolean fromNo = getIntent().getExtras().getBoolean("FROM_NO", false);
//use of ternary to make it simple
int sum = getIntent().getIntExtra(fromNo?"RESULT_SUM":"RESULT_SUMd", 0);
hi.setText(String.valueOf(sum));
}
}
答案 1 :(得分:0)
FirstActivity.java文件
EditText e=(EditText)findViewById(R.id.editText3);
EditText e2=(EditText)findViewById(R.id.editText4);`
int num1=Integer.parseInt(e.getText().toString());
int num2=Integer.parseInt(e2.getText().toString());
int sum=num1+num2;
Intent in=new Intent(this,SecondSplash.class);
in.putExtra("value",sum);
startActivity(in);
SecondActivity.java文件
TextView txtView;
String value;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_splash);
Bundle b=getIntent().getExtras();
if(b!=null)
{
value=b.getString("value");
}
txtView =(TextView)findViewById(R.id.textView2);
//value=getIntent().getExtras().getString("value");
txtView.setText(value);