MainActivity.java(头等舱)
package com.example.we.reportcardjavaclass;
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 {
public EditText one, two, three, four, five, six;
public String bang1, bang2, bang3, bang4, bang5, bang6;
public Button results;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the View that shows the numbers category
}
public void getResult(View view){
results = (Button) findViewById(R.id.result);
Intent numbersIntent = new Intent(MainActivity.this, result.class);
startActivity(numbersIntent);
one = (EditText) findViewById(R.id.name);
bang1 = one.getText().toString();
two = (EditText) findViewById(R.id.Class);
bang2 = two.getText().toString();
three = (EditText) findViewById(R.id.English);
bang3 = three.getText().toString();
four = (EditText) findViewById(R.id.Hindi);
bang4 = four.getText().toString();
five = (EditText) findViewById(R.id.Maths);
bang5 = five.getText().toString();
six = (EditText) findViewById(R.id.Science);
bang6 = six.getText().toString();
}
}
activity_main.xml(第一个布局文件)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.we.reportcardjavaclass.MainActivity"
android:padding="16dp">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Student's Name"
android:id="@+id/name"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Class"
android:id="@+id/Class"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="64dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Grade in English"
android:id="@+id/English"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="128dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Grade in Hindi"
android:id="@+id/Hindi"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="192dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Grade in Maths"
android:id="@+id/Maths"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="256dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Grade in Science"
android:id="@+id/Science"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="320dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<Button
android:text="Get result"
android:id="@+id/result"
android:onClick="getResult"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="384dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"/>
</android.support.constraint.ConstraintLayout>
</ScrollView>
## result.xml (Second Layout File) ##
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="32dp">
<android.support.v7.widget.CardView
android:id="@+id/resultCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="2dp"
android:layout_gravity="center_vertical"
card_view:cardUseCompatPadding="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="Student's Name"
android:textAllCaps="true"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:textAllCaps="false"
android:textSize="15sp"
android:textStyle="normal"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="Class"
android:textAllCaps="true"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:textAllCaps="false"
android:textSize="15sp"
android:textStyle="normal"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:background="@color/cardview_dark_background"
android:padding="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="English"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:textStyle="bold"
/>
<TextView
android:id="@+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:textStyle="normal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="40dp"
android:text="Hindi"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:textStyle="bold"
/>
<TextView
android:id="@+id/four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="40dp"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:textStyle="normal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="80dp"
android:text="Maths"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:textStyle="bold"
/>
<TextView
android:id="@+id/five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="80dp"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:textStyle="normal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="120dp"
android:text="Science"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:textStyle="bold"
/>
<TextView
android:id="@+id/six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="120dp"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:textStyle="normal"/>
</RelativeLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="350dp"
android:text="BACK TO HOME"
/>
</FrameLayout>
</android.support.v7.widget.CardView>
</LinearLayout></ScrollView>
第二课
package com.example.we.reportcardjavaclass;
import android.os.Bundle;
import android.widget.TextView;
/**
* Created by we on 08-07-2017.
*/
public class result extends MainActivity{
public TextView one, two, three, four, five, six;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
setTitle(bang1+"s Result");
one = (TextView) findViewById(R.id.one);
one.setText(bang1);
two = (TextView) findViewById(R.id.two);
two.setText(bang2);
three = (TextView) findViewById(R.id.three);
three.setText(bang3);
four = (TextView) findViewById(R.id.four);
four.setText(bang4);
five = (TextView) findViewById(R.id.five);
five.setText(bang5);
six = (TextView) findViewById(R.id.six);
six.setText(bang6);
}
}
EditText值被存储在变量bang中的MainActivity.java - 123456.我在声明它们时添加了public修饰符,所以我也可以在第二类中访问它们。在第二节课中,我想使用这些变量值进入Textviews,但在运行时却没有显示值。
在我的模拟器中运行后,我还附上了两种布局的屏幕截图。
在第二种布局中,它应该显示名称和其他值。
答案 0 :(得分:1)
我建议你退一步,更好地了解Android的基本原理,包括一般的活动。也许是一个好的开始,理解Activity Lifecycle。
对您的问题的回答并不是很简单,因为Android的工作方式可能与您的理解大不相同。
简而言之,您的第二个活动是一个完全不同于第一个活动的实例。将第二个扩展到第一个并不意味着指定的变量可用于第二个,它仅仅意味着该类以方法和字段的形式继承其父级的结构。
您应该以他们传递这些字符串的方式设计您的活动。启动第二个活动时,请在启动时在意图中包含这些字符串。
例如:
public class MainActivity extends AppCompatActivity {
// ...
/** Your method where the second activity is launched */
public void onButtonClicked() {
Intent intent = new Intent(this, SecondActivity.class);
intent.putString(SecondActivity.EXTRA_BANG1, bang1);
// ...
startActivity(intent);
}
}
public class SecondActivity extends AppCompatActivity {
public static final String EXTRA_BANG1 = "bang1";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
// ...
TextView tvOne = (TextView) findViewById(R.id.one);
// ...
String bang1 = getIntent().getString(EXTRA_BANG1);
tvOne.setText(bang1);
}
}
我完全建议您阅读Android文档(它写得非常好!),以便通过此机制更好地了解它的工作原理(以及原因)。特别是,this article on Sending data between activities与您非常相关。
答案 1 :(得分:-2)
如果要在其他活动中使用这些值,则只需将这些值与用于开始新活动的意图一起传递。
MainActivity.java
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("Bang1",bang1);
i.putExtra("Bang2",bang2);
i.putExtra("Bang3",bang3);
...
你可以在SecondActivity.java文件中获取它们:
Intent i =getIntent();
int bang1 = i.getIntExtra("Bang1");
...
但是如此选择字符串名称,我建议创建一个新类来存储字符串名称作为最终字符串常量,以避免任何错误。
或者您可以直接将这些变量声明为静态,并通过将其名称引用为SecondActivity来访问它们:
int bang1 = MainActivity.bang1;