80,490
社区成员
发帖
与我相关
我的任务
分享package com.example.ch02_marry;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt1:
Intent intent=new Intent();
intent.setClass(MainActivity.this,ResultActivity.class);
MainActivity.this.startActivityForResult(intent,0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
package com.example.ch02_marry;
import android.app.Activity;
import android.os.Bundle;
public class ResultActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result_main);
}
}
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ch02_marry.MainActivity"
android:stretchColumns="2">
<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:layout_span="3"
android:text="@string/app_name"
android:textSize="35sp"/>
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/laogong"
android:textSize="25sp"/>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/mingzi"
android:textSize="35sp" />
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"/>
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/dianhua"
android:textSize="15sp"/>
<EditText
android:inputType="text"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_span="2"
android:gravity="center"
android:textSize="15sp"/>
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/chusheng"
android:textSize="15sp"/>
<EditText
android:inputType="text"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_span="2"
android:gravity="center"
android:textSize="15sp"/>
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/zhuzhi"
android:textSize="15sp"/>
<EditText
android:inputType="text"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_span="2"
android:gravity="center"
android:textSize="15sp"/>
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/laopo"
android:textSize="25sp"/>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/ming"
android:textSize="35sp" />
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"/>
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/dianhua"
android:textSize="15sp"/>
<EditText
android:inputType="text"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_span="2"
android:gravity="center"
android:textSize="15sp"/>
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/chusheng"
android:textSize="15sp"/>
<EditText
android:inputType="text"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_span="2"
android:gravity="center"
android:textSize="15sp"/>
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/zhuzhi"
android:textSize="15sp"/>
<EditText
android:inputType="text"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_span="2"
android:gravity="center"
android:textSize="15sp"/>
</TableRow>
<TableRow>
<Button
android:id="@+id/bt1"
android:layout_height="wrap_content"
android:layout_span="3"
android:gravity="center"
android:text="@string/marry"
android:textSize="20sp" />
</TableRow>
</TableLayout>
<?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="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ch02_marry.MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/wenben"
android:textSize="45sp"/>
</LinearLayout> <?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">结婚证</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="marry">结婚</string>
<string name="nam">名字</string>
<string name="laogong">丈夫</string>
<string name="laopo">妻子</string>
<string name="mingzi">悟空</string>
<string name="dianhua">电话</string>
<string name="chusheng">出生日期</string>
<string name="zhuzhi">住址</string>
<string name="ming">八戒</string>
<string name="wenben">祝白头到老</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ch02_marry"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.ch02_marry.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.ch02_marry.ResultActivity"
android:label="@string/app_name"
></activity>
</application>
</manifest>


package com.example.ch02_marry;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.bt1);
button.setOnClickListener(this);
TextView textView = (TextView) findViewById(R.id.tv1);
textView.setText(Html.fromHtml(descString(), getImageGetterInstance(),null));
TextView textView2 = (TextView) findViewById(R.id.tv2);
textView2.setText(Html.fromHtml(descStrings(), getImageGetterInstance(),null));
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt1:
Intent intent=new Intent();
intent.setClass(MainActivity.this,ResultActivity.class);
MainActivity.this.startActivityForResult(intent,0);
break;
default:break;
}
}
private String descString() {
return "<img src='" + R.drawable.monkey + "'/>";
}
private String descStrings() {
return "<img src='" + R.drawable.pig + "'/>";
}
public ImageGetter getImageGetterInstance() {
ImageGetter imgGetter = new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
int id = Integer.parseInt(source);
Drawable d = getResources().getDrawable(id);
int wdp = dip2px(getApplicationContext(), 100);
int hdp = dip2px(getApplicationContext(), 58);
Log.e("wdp", wdp + "");
Log.e("hdp", hdp + "");
d.setBounds(0, 0, wdp, hdp);
return d;
}
};
return imgGetter;
}
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
else if (id==R.id.zhuxiao_item) {
Toast toast1=Toast.makeText(getApplicationContext(),"你已离婚了!",Toast.LENGTH_SHORT);
toast1.show();
}
else if (id==R.id.chongban_item) {
Toast toast1=Toast.makeText(getApplicationContext(),"你已复婚了!",Toast.LENGTH_SHORT);
toast1.show();
}
return super.onOptionsItemSelected(item);
}
}
<menu 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"
tools:context="com.example.ch02_marry.MainActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
<item
android:id="@+id/zhuxiao_item"
android:title="@string/zhuxiao"/>
<item
android:id="@+id/chongban_item"
android:title="@string/chongban"/>
</menu>

先获取控件,findViewById