Android 基础一 TextView,Style样式,Activity 传值,选择CheckBox 显示密码

时间:2022-05-17 01:19:36

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 stub
super.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());;
  }
}
})