標籤:
1.修改TextView字型
mTextView = (TextView) findViewById(R.id.textview1);mTextView.setText("I am here");Resources resources = getBaseContext().getResources(); Drawable myDrawable = resources.getDrawable(R.drawable.Drawable1);mTextView.setBackgroundDrawable(myDrawable);
mTextView.setTextSize(20);
mTextView.setTextColor(Color.WHITE);
2.擷取Android手機 螢幕解析度
DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);String spt = "手機解析度為"+dm.widthPixels+"x"+dm.heightPixels;
3.定義Style樣式
<TextView style="@style/myStyle_tv1" android:id="@+id/startapp_tv" android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#00BB00" android:gravity="center" android:text="@string/startapp_tv1 />
然後在res/values/style.xml下加入
<resources><style name="myStyle_tv1"><item name="android:textSize">18sp</item><item name="android:textColor">#ffffff</item><style></resources>
4.不同Activity之間傳遞資料
(1)activity01中Intent intent = new Intent();intent.setClass(activity.this, activity2.class);Bundle bundle = new Bundle();bundle.putString("str_var1", "Hello");bundle.putDouble("dob_var1", 2.333);intent.putExtras(bundle);startActivity(intent);(2)activity02中Bundle bundle = this.getIntent().getExtras();string str = bundle.getString("str_var1");double db = bundle.getDouble("dob_var1");
5.startActivityForResult
(1)activity01中Intent intent = new Intent();intent.setClass(activity.this, activity2.class);Bundle bundle = new Bundle();bundle.putString("str_var1", "Hello");bundle.putDouble("dob_var1", 2.333);intent.putExtras(bundle);startActivityForResult(intent, 1);protected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);switch(requestCode){case 1://取得activity02的返回的資料 Bundle bundle2 = data.getExtras(); String str = bundle2.getString("str_var2"); double db = bundle2.getDouble("dob_var2");break;default:break;}}(2)activity02中Bundle bundle = this.getIntent().getExtras();string str = bundle.getString("str_var1");double db = bundle.getDouble("dob_var1");string str2 = "activity02 data";double db2 = 3.3333;
Button btn = (Button)findViewById(R.id.btn1);
btn.setOnClickListener(new Listener());
//定義一個監聽按鈕的類,這樣以後可以處理多個按鈕事件
class Listener implements OnClickListener{
public void onClick(View v){
switch(v.getId()){
case R.id.btn1:
Intent intent = new Intent();
intent.setClass(this, activity02.class);
Bundle bd = new Bundle();
bd.putString("str_var2", str2);
bd.putDouble("dob_var2", db2);
intent.putExtras(bd);
activity02.this.setResult(1, intent);
this.finish();
break;
}
}
}
6.選擇CheckBox 顯示密碼
在OnCreate方法中
edit = (EditText)findViewById(R.id.pwd_edit);checkbox = (CheckBox)findViewById(R.id.pwd_checkbox);checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // TODO Auto-generated method stub if(checkbox.isChecked()){ edit.setTransformationMethod(HideReturnsTransformationMethod.getInstance());; } else{
edit.setTransformationMethod(PasswordTransformationMethod.getInstance());; } }
})
Android 基礎一 TextView,Style樣式,Activity 傳值,選擇CheckBox 顯示密碼