Google Android SDK开发范例大全笔记 一

时间:2024-04-21 15:38:43

方法讲解

1 获取手机分辨率方法 DisplayMetrics

private void getDiaplayMetrics()
{
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
String strMetrics = "手机分辨率为:"+dm.widthPixels+" * "+dm.heightPixels;
String strdm =" 手机屏幕信息:" + dm.toString();
Log.d("panzqww",strMetrics);
Log.d("panzqww",strdm);
}

结果

手机分辨率为:720 * 1280
手机屏幕信息:DisplayMetrics{density=2.0, width=720, height=1280, scaledDensity=2.0, xdpi=320.0, ydpi=320.0}

2 startActivityForResult 方法

  • MainActivity中执行跳转到Activity_b
Intent intent = new Intent(MainActivity.this,Activity_b.class);
startActivityForResult(intent,REQUEST_CODE);
  • Activity_b中执行 将Activity_b finish掉返回到MainActivity
Intent intent = new Intent();
intent.putExtra("result","来自Activity b");
Activity_b.this.setResult(RESULT_CODE,intent);
Activity_b.this.finish();
  • MainActivity中onActivityResult接收到返回的数据
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data !=null)
{
String result = data.getExtras().getString("result");
Log.d("panzqww","requestCode = " + requestCode + ", resultCode = " + resultCode + " , result = " + result);
}
}

打印信息

requestCode = 256, resultCode = 512 , result = 来自Activity b

3 解析并获取asset目录下的 ttf字体样式

tv_show.setTypeface(Typeface.createFromAsset(getAssets(), "BinnerD.ttf"));

https://github.com/MichealPan9999/android_sdk_test

4 menu菜单

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
openOptionDialogAbout();
break;
case 1:
openOptionDialogExit();
break;
}
return true;
} private void openOptionDialogAbout() {
new AlertDialog.Builder(this)
.setTitle(R.string.about)
.setMessage("版本号 V1.1.8")
.setPositiveButton(R.string.confirm
, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) { }
}).show();
}
private void openOptionDialogExit() {
new AlertDialog.Builder(this)
.setTitle(R.string.exit)
.setMessage("确定要退出吗")
.setPositiveButton(R.string.yes
, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
})
.setNegativeButton(R.string.no,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) { }
}).show();
}

执行完以后会在应用右上角出现标志Google Android SDK开发范例大全笔记 一

点击该标志出现两个选项

Google Android SDK开发范例大全笔记 一

5 EditText和setTransformationMethod实现密码框显示密码和隐藏密码功能

 et_password = findViewById(R.id.et_password);
cb_show_hide = findViewById(R.id.cb_show_hide);
cb_show_hide.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){ @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (cb_show_hide.isChecked())
{
et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
cb_show_hide.setText("隐藏密码");
}else{
et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
cb_show_hide.setText("显示密码");
}
}
});

6 隐藏是抽屉 SlidingDrawer

public class SlidingDrawerActivity extends AppCompatActivity {

    private GridView gv;
private SlidingDrawer sd;
private ImageView im;
private int[] icons = {R.drawable.apple_pic,R.drawable.banana_pic,R.drawable.cherry_pic};
private String[] items = {"苹果","香蕉","樱桃"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sliding_drawer);
gv = findViewById(R.id.gv_myGridView);
sd = findViewById(R.id.sd_mySlidingDrawer);
im = findViewById(R.id.iv_myImage);
MyGridviewAdapter adapter = new MyGridviewAdapter(this,items,icons);
gv.setAdapter(adapter);
sd.setOnDrawerOpenListener(new OnDrawerOpenListener() {
@Override
public void onDrawerOpened() {
im.setImageResource(R.drawable.open);
}
});
sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
@Override
public void onDrawerClosed() {
im.setImageResource(R.drawable.close);
}
});
}
class MyGridviewAdapter extends BaseAdapter{

    private Context mContext;
private String[] _items;
private int[] _icons; public MyGridviewAdapter(Context mContext, String[] _items, int[] _icons) {
this.mContext = mContext;
this._items = _items;
this._icons = _icons;
} @Override
public int getCount() {
return _items.length;
} @Override
public Object getItem(int i) {
return _items[i];
} @Override
public long getItemId(int i) {
return i;
} @Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater factory = LayoutInflater.from(mContext);
View v = factory.inflate(R.layout.grid,null);
ImageView iv = v.findViewById(R.id.icon);
TextView tv = v.findViewById(R.id.text);
iv.setImageResource(_icons[i]);
tv.setText(_items[i]);
return v;
}

布局 activity_sliding_drawer

<?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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <SlidingDrawer
android:id="@+id/sd_mySlidingDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:content="@+id/gv_myGridView"
android:handle="@+id/layout1"
android:orientation="horizontal"> <LinearLayout
android:id="@id/layout1"
android:layout_width="35px"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"> <ImageView
android:id="@+id/iv_myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_pic"/> </LinearLayout> <GridView
android:id="@+id/gv_myGridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:numColumns="2" />
</SlidingDrawer> </LinearLayout>

grid

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

修改 SlidingDrawer中的orientation可以更改图标方向

<SlidingDrawer
android:id="@+id/sd_mySlidingDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:content="@+id/gv_myGridView"
android:handle="@+id/layout1"
android:orientation="vertical">

7 Linkify 将内容识别成 网址 电话 E-mail

根据输入的内容自动识别成网址 电话 E-mail 点击自动跳转

public class LindifyActivity extends AppCompatActivity {

    private TextView mTvContent;
private EditText mEtContent;
private Button mBtnLinkify; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lindify);
mTvContent = findViewById(R.id.tv_content);
mEtContent = findViewById(R.id.et_linkify);
mBtnLinkify = findViewById(R.id.btn_Linkify);
mBtnLinkify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
linkify();
}
});
} private void linkify() {
mTvContent.setText(mEtContent.getText());
Linkify.addLinks(mTvContent,
Linkify.WEB_URLS |
Linkify.EMAIL_ADDRESSES |
Linkify.PHONE_NUMBERS);
}
}

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
style="@style/MyStyle"
android:text="请输入电话/E-mail/网址" />
<EditText
android:id="@+id/et_linkify"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_content"
style="@style/MyStyle" />
<Button
android:id="@+id/btn_Linkify"
style="@style/MyStyle"
android:text="自动链接内容" /> </LinearLayout>

8 电话号码的正则表达式

所有手机号码:regexp="^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$";