我对APP有一个简单的想法,而且我是初学者。我的想法是创建一个有2个活动的应用程序。第一个活动将要求用户输入他/她的名字(String)和重量(Double),然后用户将点击一个按钮,将数据传输到Activity 2,它将显示名称,重量将成倍增加由0.0425。这是我到目前为止的代码。
活动1:
package com.example.abdulelah.newapp1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText editText4;
EditText editText3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText = (EditText) findViewById(R.id.editText);
final EditText editText1 = (EditText) findViewById(R.id.editText4);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editText.getText().toString();
Double value1 = Double.parseDouble(editText4.getText().toString());
Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
intent.putExtra("myname", name);
intent.putExtra("weight", value1);
startActivity(intent);
}
}
);
}
}
活动2:
package com.example.abdulelah.newapp1;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.widget.EditText;
import android.widget.TextView;
public class Main2Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView textView = (TextView)findViewById(R.id.editText2);
EditText editText =(EditText)findViewById(R.id.editText3);
Intent intent = getIntent();
String name = intent.getStringExtra("myname");
Double value1 = intent.getDoubleExtra("weight", 0);
textView.setText(name);
editText.setText((int) (value1*0.0425));
}
}
等待宝贵的帮助。