一、动态改变控件大小
测试按钮、编辑框、textview、滑块等控件都适合改变大小
Button bt = (Button)this.findViewById(R.id.Button01);
LinearLayout.LayoutParams lParams = (LinearLayout.LayoutParams);
bt.getLayoutParams();
lParams.width=100;//宽度
lParams.height=40;//高度
bt.setLayoutParams(lParams);
二、获取屏幕分辨率
final DisplayMetrics dm = new DisplayMetrics();
//在非activity中使用getWindowManager
((Activity)getContext()).getgetWindowManager().getDefaultDisplay().getMetrics(dm);
//在activity中使用getWindowManager
getWindowManager().getDefaultDisplay().getMetrics(dm);
dm.widthPixels为屏幕宽度
dm.heightPixels为屏幕高度
三、控件直接间隔
水平摆放时,用android:layout_marginLeft = "10dp"左间隔10dip
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
四、对话框
import android.app.AlertDialog;
new AlertDialog.Builder(this) //创建对话框
.setTitle("冬季保健小常识") //设置对话框标题
.setMessage("教你如何快乐过冬!") //设置对话框内容
.setPositiveButton("确定", null) //为对话框添加按钮
.show(); //显示对话框
五、eclipse快捷键
联想补全:Alt+/
对齐:Ctrl+Shift+F
向左移:Shitf+Tab
向右移:Tab
六、关于标签页控件的操作
因为标签页分为很多个XML组成,所有标签页的控件ID最好不要一样,一样的ID在操作时,只会操作最前页中相同ID的控件,操作方式和平时一般控件一样。
七、EditText
获取数据:edit.getText().toString();
设置为不可编辑:edit.setEnabled(false);//true为可用
android:digis="123456789"//限制规则,表示编辑框中只能输入123456789这9个数字
八、TextView
显示数据:tv.setText("显示的数据");//tv为初始化名称
获取数据:tv.getText().toString();
响应单击:
android:clickable="true"
tv1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
//弹出对话框,在对话框中输入数据,然后传递到文本框里面显示,更改美观
}
});
九、Toast中设置显示时长,设置第三个参数
toast.LENGTH_SHORT 为2秒
toast.LENGTH_LONG 为3.5秒
十、对话框
final EditText et = new EditText(MainActivity.this); //带编辑框的对话框
/****限制输入数字******/
et.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3){}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3){}
@Override
public void afterTextChanged(Editable arg0)
{
try
{
String temp = arg0.toString();
String tem = temp.substring(temp.length()-1, temp.length());
char[] temC = tem.toCharArray();
int mid = temC[0];
if(mid>=48 && mid<=57 || mid == 46)
{//数字
return;
}
arg0.delete(temp.length()-1, temp.length());
}
catch (Exception e)
{
}
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {}
@Override
public void afterTextChanged(Editable arg0)
{
edit(arg0,2);
}
});
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("对话框名称");
dialog.setMessage("二号名称");
dialog.setCancelable(false);
dialog.setView(et);
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0, int arg1)
{
String input = et.getText().toString();
if (input.equals(""))
toask("空");
else
toast("ok");
}
});
dialog.setNegativeButton("cancel", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0, int arg1)
{
toast("cancel");
}
});
dialog.show();
十一、数学函数
Math.pow(x,y);//x的y次幂
十二、
ctrl+F11 快捷键运行程序
十三、控件
setEnabled(false)//设置控件为不可用
setEnabled(true)//设置控件为可用
setText("填写数据")//设置内容,“”为清空
clearFocus()//设置焦点不在控件上
.getText().toString();//获取编辑框数据
十四、
private wifi wi;
wi = new wifi(MainActivity.this);
wi.方法()
十五、显示菜单,解决没有菜单按钮问题
程序界面上点击菜单按钮弹出菜单,
修改AndroidManifest.xml中为Android:targetSdkVersion="12",程序界面就会出现菜单按钮
十六、控件隐藏设置
XML中
android:visibility="invisible"//隐藏,但是占用屏幕空间
android:visibility="gone"//隐藏,但是不占用屏幕空间
android:visibility="visible"//显示
代码中
bt.setVisibility(View.VISIBLE);//显示
十七、double转化为string函数
private String format(double num) //保留两位小数
{
NumberFormat formatter = new DecimalFormat("0.##");
String str = formatter.format(num);
return str;
}
十八、非activity中获取context、布局文件等
在非activity中获取activity的context:((Activity) getContext()),别的方法都有问题
获取布局文件:
LayoutInflater factory = LayoutInflater.from(((Activity) getContext()));
View layout = factory.inflate(R.layout.activity_main, null);
Mapp view = (Mapp)layout.findViewById(R.id.map);//map布局是在activity_main中的
PS:使用view.invalidate()没有响应
十九、动态改变活动背景
final LinearLayout layout1 = (LinearLayout)findViewById(R.id.layout1); //注意LinearLayout
layout1.setBackgroundResource(R.drawable.no);
layout1.setBackgroundResource(R.drawable.off);
二十、设置屏幕亮度
Window window = Sos.this.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = (float) 1.0;//0.0-1.0
window.setAttributes(lp);
二十一、背景图和状态栏融合到一起的效果
oncreate中添加:
if(Build.VERSION.SDK_INT >= 21) { View decorView = getWindow().getDecorView(); decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); getWindow().setStatusBarColor(Color.TRANSPARENT); }XML中布局添加
android:fitsSystemWindows="true"
PS:eclipse中
//透明任务栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明导航栏
//getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);