Activity(縁結びのまとめ)
19008 ワード
コードは次のとおりです.
レイアウト1:
レイアウト1:
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/name1"
android:hint=" "
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/name2"
android:hint=" "
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text=" "
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"/>
LinearLayout>
布局二:xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/ceshi" android:layout_width="match_parent" android:layout_height="wrap_content" /> LinearLayout>
MainActivity.java:
package com.caozhihong.dell.test2; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class MainActivity extends AppCompatActivity { private EditText etYourName; private EditText etOtherNmae; String yourname; String othername; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etYourName=(EditText)findViewById(R.id.name1); etOtherNmae=(EditText)findViewById(R.id.name2); } public void click(View v){ Intent intent=new Intent(); intent.putExtra("yourname",etYourName.getText().toString()); intent.putExtra("othername",etOtherNmae.getText().toString()); intent.setClass(MainActivity.this,Activity2.class); startActivity(intent); } }
Activity2.java:package com.caozhihong.dell.test2; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; import java.util.Random; /** * Created by dell on 2016/1/3. */ public class Activity2 extends Activity { private TextView tvyinyuan; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout2); tvyinyuan=(TextView)findViewById(R.id.ceshi); Intent intent=getIntent(); String yourName=intent.getStringExtra("yourname"); String otherName=intent.getStringExtra("othername"); Random random=new Random(); int num=random.nextInt(100); tvyinyuan.setText(yourName+" "+otherName+" "+num); } }
:
1.setContentView(R.layout.activity_main);
「setContentView」を使用して、onCreate関数にレイアウトを追加します.
2.EditTextタイプのetYourNameとetOtherNameを宣言して、レイアウトのEditTextに連絡します.
3.通過etYourName=(EditText)findViewById(R.id.name1); etOtherNmae=(EditText)findViewById(R.id.name2); etYourName etOtherName 。
4.
android:onClick="click"
public void click(View v){ Intent intent=new Intent(); intent.putExtra("yourname",etYourName.getText().toString()); intent.putExtra("othername",etOtherNmae.getText().toString()); intent.setClass(MainActivity.this,Activity2.class); startActivity(intent); }
。
5. intent “ ”, intent 。
6. intent.setClass(MainActivity.this,Activity2.class); 。
7. startActivity(intent); Activity2。
8.Activity2 ,
private TextView tvyinyuan; , tvyinyuan.setText(yourName+" "+otherName+" "+num); , 。
9.
Random random=new Random(); int num=random.nextInt(100); , num。
10.
MainActivity private EditText etYourName; String yourname; intent.putExtra("yourname",etYourName.getText().toString()); Activity String yourName=intent.getStringExtra("yourname"); 。
:
1. 。
2. manifests Activity2:
<activity android:name=".Activity2">activity>