Activity的跳转
Activity的跳转
首先是简单的布局
主布局
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et"
android:hint="@string/xm"
android:layout_marginTop="250dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:onClick="onClick"
android:id="@+id/pg"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/pg"/>
<Button
android:onClick="onClick"
android:id="@+id/tc"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="@string/tc"/>
</LinearLayout>
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""/>
子布局
<TextView
android:id="@+id/f"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:onClick="onClick"
android:id="@+id/y"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="@string/y"
/>
<Button
android:onClick="onClick"
android:id="@+id/n"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="@string/n"
/>
</LinearLayout>
实现的
主代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xm = (EditText) findViewById(R.id.et);
result = (TextView) findViewById(R.id.result);
Intent intent=getIntent();
result.setText(intent.getStringExtra("data"));
}
public void onClick(View view) {
String name = xm.getText().toString();
switch (view.getId()) {
case R.id.pg:
pg();
case R.id.tc:
finish();
break;
}
}
private void pg() {
Intent intent = new Intent(MainActivity.this, Main2.class);
String text = xm.getText().toString();
intent.putExtra("name", text + ":对学习java有没有信心?");
startActivity(intent);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
f = (TextView) findViewById(R.id.f);
Intent intent = getIntent();
String name = intent.getStringExtra("name");
f.setText(name + ":" + "对学习java有没有信心?");
}
public void onClick(View view){
switch (view.getId()){
case R.id.y:
y();
break;
case R.id.n:
n();
break;
}
}
public void y(){
Intent intent = new Intent(Main2.this,MainActivity.class);
String yes = y.getText().toString();
intent.putExtra("result","对学习java有信心");
startActivity(intent);
finish();
}
public void n(){
Intent intent = new Intent(Main2.this,MainActivity.class);
String no= n.getText().toString();
intent.putExtra("result","对学习java没有信心");
startActivity(intent);
finish();
}
}
主要
1.传递数据
result.setText(intent.getStringExtra("data"));
2.实现界面跳转
Intent intent = new Intent(MainActivity.this, Main2.class);
3.获取数据并显示
Intent intent = getIntent();
String yes = y.getText().toString();
intent.putExtra("result","对学习java有信心");
startActivity(intent);
finish();