标签:
背景图片自己替换或者改成背景颜色。
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_boyname; private EditText et_girlname; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_boyname = (EditText) this.findViewById(R.id.et_boyname); et_girlname = (EditText) this.findViewById(R.id.et_girlname); } public void btn_calc(View view) { String boyname = et_boyname.getText().toString(); String girlname = et_girlname.getText().toString(); if (TextUtils.isEmpty(boyname)) { Toast.makeText(this, "男生姓名不能为空", 0).show(); et_boyname.setFocusableInTouchMode(true); et_boyname.requestFocus(); et_boyname.requestFocusFromTouch(); return; } if (TextUtils.isEmpty(girlname)) { Toast.makeText(this, "女生姓名不能为空", 0).show(); et_girlname.setFocusableInTouchMode(true); et_girlname.requestFocus(); et_girlname.requestFocusFromTouch(); return; } Intent intent = new Intent(this, SecondActivity.class); intent.putExtra("boyname", boyname); intent.putExtra("girlname", girlname); startActivity(intent); } }
mport android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class SecondActivity extends Activity { private TextView tv_result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); tv_result = (TextView) this.findViewById(R.id.tv_result); Intent intent = getIntent(); if (intent != null) { String boyname = intent.getStringExtra("boyname"); String girlname = intent.getStringExtra("girlname"); int hashcode = boyname.hashCode() + girlname.hashCode(); Random random = new Random(hashcode); int ran = random.nextInt(100); tv_result.setText(boyname + "和" + girlname + "的姻缘值:" + ran); } } }
<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:orientation="vertical" tools:context=".MainActivity" android:gravity="center_vertical" android:background="@drawable/aiqing"> <EditText android:id="@+id/et_boyname" android:layout_width="match_parent" android:layout_height="40dp" android:hint="请输入男孩姓名"/> <EditText android:layout_marginTop="30dp" android:id="@+id/et_girlname" android:layout_width="match_parent" android:layout_height="40dp" android:hint="请输入女孩姓名"/> <Button android:layout_marginTop="30dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:gravity="center_vertical|center_horizontal" android:onClick="btn_calc" android:text="开始计算姻缘" /> </LinearLayout>
<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:background="@drawable/jieguo" android:orientation="vertical" tools:context=".SecondActivity" > <TextView android:gravity="center_vertical" android:id="@+id/tv_result" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#ff0000" android:textSize="18sp" android:text="姻缘值" /> </LinearLayout>
运行截图:
标签:
原文地址:http://www.cnblogs.com/shen-hua/p/5694875.html