main.xml文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/textView1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<Button android:id="@+id/btnTV" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/btn" />
</LinearLayout>
test.xml文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/tvSecond" />
</LinearLayout>
对应Main.java内容如下:
public class Main extends Activity implements OnClickListener {
private Button btnTV;
private TextView textView1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
LinearLayout mainLinearLayout = (LinearLayout) this.getLayoutInflater()
.inflate(R.layout.main, null);
setContentView(mainLinearLayout);
btnTV = (Button) this.findViewById(R.id.btnTV);
textView1 = (TextView) this.findViewById(R.id.textView1);
btnTV.setOnClickListener(this);
LinearLayout testLinearLayout = (LinearLayout) this.getLayoutInflater()
.inflate(R.layout.test, mainLinearLayout);
EditText editText = new EditText(this);
editText.setSingleLine(false);
editText.setGravity(Gravity.LEFT);
mainLinearLayout.addView(editText, new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
@Override
public void onClick(View v) {
int value = textView1.getGravity() & 0x07;
if (value == Gravity.LEFT) {
textView1.setGravity(Gravity.CENTER_HORIZONTAL);
} else {
if (value == Gravity.CENTER_HORIZONTAL) {
textView1.setGravity(Gravity.RIGHT);
} else {
textView1.setGravity(Gravity.LEFT);
}
}
}
}
进入联系人界面
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(People.CONTENT_URI);
startActivity(intent);
查看某个联系人,当然这里是ACTION_VIEW,如果为选择并返回action改为ACTION_PICK,当然处理intent时返回需要用到startActivityforResult
Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, ID);//最后的ID参数为联系人Provider中的数据库BaseID,即哪一行
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(personUri);
startActivity(intent);
选择一个图片
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");//此处->audio/*对应选择音频 video/*对应选择视频
startActivityForResult(intent, 0);
调用Android设备的照相机,并设置拍照后存放位置
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment
.getExternalStorageDirectory().getAbsolutePath()+"/cwj", android123 + ".jpg"))); //存放位置为sdcard卡上cwj文件夹,文件名为android123.jpg格式
startActivityForResult(intent, 0);
搜索指定package name在market上,比如搜索com.android123.cwj的写法如下
Uri uri = Uri.parse("market://search?q=pname:com.android123.cwj");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
本文转载自:http://www.android123.com.cn/androidkaifa/629.html
下面是我们所涉及有变动的代码:
首先是主页面布局main.xml 和第二个页面的布局mylayout.xml
main.xml 这里我们用了AbsoluteLayout布局大家 要注意哦
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Person information"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Name:"
android:layout_y="30px"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Gender:"
android:layout_y="80px"
/>
<EditText
android:id="@+id/ed1"
android:layout_width="200px"
android:layout_height="wrap_content"
android:layout_x="50px"
android:layout_y="20px"
/>
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="man"
android:layout_x="60px"
android:layout_y="65px"
android:checked="true"//默认它是被选中的
/>
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="woman"
android:layout_x="160px"
android:layout_y="65px"
/>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="confirm"
android:layout_y="120px"
/>
</AbsoluteLayout>
在main.xml 同一目录下建另外一个页面布局mylayou.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/mytv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
下面是程序的核心代码BundleDemo.java 和BundleDemo1.java
BundleDemo.java:
package com.android.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
public class BundleDemo extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//以findViewById()取得Button对象,并添加响应
Button bt1 = (Button)findViewById(R.id.bt1);
bt1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
//取得name
EditText et = (EditText)findViewById(R.id.ed1);
String name = et.getText().toString();
//取得Gender
String sex="";
RadioButton rb1 = (RadioButton)findViewById(R.id.rb1);
if(rb1.isChecked())
{
sex="man";
}else{
sex="woman";
}
//new一个Intent对象,用法上节已经讲过了
Intent intent = new Intent();
intent.setClass(BundleDemo.this, BundleDemo1.class);
//new一个Bundle对象,并将要传递的数据导入,这里是重点Map<String,String>结构哦
Bundle bundle = new Bundle();
bundle.putString("name",name);
bundle.putString("sex", sex);
//将Bundle对象assign给Intent
intent.putExtras(bundle);
//调用Activity BundleDemo1
startActivity(intent);
}
});
}
}
在BundleDemo.java 同一目录建立BundleDemo1.java :
package com.android.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class BundleDemo1 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
//取得Intent中的Bundle对象
Bundle bundle = this.getIntent().getExtras();
//取得Bundle对象中的数据
String name = bundle.getString("name");
String sex = bundle.getString("sex");
//设置输出文字
TextView mytv = (TextView)findViewById(R.id.mytv);
mytv.setText("you name is:" + name + "\n you gender is:"+sex);
}
}
最后我们还是要在AndroidManifest.xml 中加入BundleDemo1 这个Activity ,一定要加,不然后果自负呵呵~
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BundleDemo"
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="BundleDemo1"></activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
执行之,将达到上述效果~今天到此结束,谢谢大家!