//1.使用findViewById找到布局文件里的控件对象
mTextViewOne = (TextView) findViewById(R.id.textViewOne);
//2.设置TextView显示的文字
mTextViewOne.setText(str);
//3.布局文件中TextView控件的id,以及显示文字
android:id="@+id/textViewOne"
android:id="@string/str"
//4.布局文件中TextView显示各种链接,如:http://www.baidu.com
android:autoLink="all"
//5.布局文件中改变控件的背景
android:background="@drawable/white"
//6.布局文件中改变控件的字体颜色
android:textColor="@drable/darkgray"
//7.获取资源对象,设置背景
Resources res = getBaseContext().getResources();
Drawable draw = (Drawable)res.getDrawable(R.drawable.white);
mText.setBackgroundDrawable(draw);
//8.获取资源文件中定义的字符串
CharSequence str = getString(R.string.str);
//9.在values/strings.xml中定义字符串时,如果含有"?","'","\"等符号,必须使用转义字符(\)
\?
\'
\\
//10.通过DisplayMetrics获取手机的屏幕分辨率
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
mText.setText("本手机屏幕分辨率是:" + dm.widthPixels + "x" + dm.heightPixels);
//11.布局中设置控件的样式
style="@style/styleTest2"
//12.处理按钮事件
mButtonOne.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
//ToDo
}
});
//13.设置页面布局
setContentView(R.layout.main);
//14.调用另一个Activity
Intent intent = new Intent();
intent.setClass(MeActivity.this, YouActivity.class);//指定要启动的Class
startActivity(intent);//调用一个新的Activity
MeActivity.this.finish();//结束本Activity
//15.每一个Activity都需要在AndroidManifest.xml中声明
<activity
android:name=".TextBackgroundDrawable_20120901Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//16.在Activity之间传递数据
//Intent
Intent intent = new Intent(TextBackgroundDrawable_20120901Activity.this, AnotherActivity.class);
//数据
Bundle bundle = new Bundle();
bundle.putString("string", "value");
bundle.putInt("int", 23);
//绑定数据
intent.putExtras(bundle);
//启动Activity
startActivityForResult(intent, REQUESTCODE_MAIN);
//17.处理Activity的返回(和startActivityForResult函数有关)的函数
onActivityResult(......)
//18.使用字体
mText.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf"));
//19.AlertDialog 对话框
new AlertDialog.Builder(this)
.setTitle("关于")//标题
.setMessage("About...")//内容
.setCancelable(false)//设定不可取消
.setIcon(R.drawable.ic_launcher)//图标
.setPositiveButton("知道了", new DialogInterface.OnClickListener() {//按钮1
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(GalleryActivity.this, "知道了~", Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("管他呢", new DialogInterface.OnClickListener() {//按钮2
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(GalleryActivity.this, "管他呢~", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("不知道!", new DialogInterface.OnClickListener() {//按钮3
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(GalleryActivity.this, "不知道~", Toast.LENGTH_SHORT).show();
}
})
.create()
.show();//显示
//20.菜单使用(重载Activity中的两个函数)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//添加菜单
menu.add(0, 0, 1, "关于");//(groupId, itemId, order, text)
menu.add(0, 1, 0, "退出");
return super.onCreateOptionsMenu(menu);
}
//处理菜单点击事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case 0:
openOptionDialog();
break;
case 1:
finish();
break;
}
return super.onOptionsItemSelected(item);
}