疯狂安卓书籍的学习--未完待续~
来源:互联网 发布:新浪微博登陆网络异常 编辑:程序博客网 时间:2024/06/05 22:04
疯狂安卓书籍的学习
如何自定义view
先建一个view类 重写ondraw方法
public class DrawView extends View { public float currentX = 40; public float currentY = 50; // 定义并创建画笔 Paint paint = new Paint(); public DrawView(Context context) { super(context); // TODO Auto-generated constructor stub } public DrawView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); //设置画笔的颜色 paint.setColor(Color.GREEN); canvas.drawCircle(currentX, currentY, 15, paint); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub currentX=event.getX(); currentY=event.getY(); invalidate();//重新绘制 //表名 该处理方法已经处理 return true; } }再在activity中将view类添加进去 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RelativeLayout view=(RelativeLayout) findViewById(R.id.root); DrawView drawView=new DrawView(this); view.addView(drawView); }也可以直接在布局文件中添加这个自定义控件 <RelativeLayout 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:id="@+id/root" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.android.MainActivity" > <com.example.android.DrawView android:id="@+id/diydraw" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout>
效果如下
计算器
布局文件如下
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:rowCount="6" android:columnCount="4" android:id="@+id/root" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_columnSpan="4" android:textSize="50dp" android:layout_marginLeft="2pt" android:layout_marginRight="2pt" android:layout_gravity="right" android:text="0"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_columnSpan="4" android:text="清除"/> </GridLayout>
activity文件如下
public class MainActivity extends Activity { GridLayout girdlayout; String[] omg = { "1", "2", "3", "/", "4", "5", "6", "*", "7", "8", "9", "-", ".", "0", "=", "+", }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.caculator); girdlayout = (GridLayout) findViewById(R.id.root); for (int i = 0; i < omg.length; i++) { Button button = new Button(this); button.setText(omg[i]); button.setTextSize(40); button.setPadding(5, 35, 5, 35); //指定该组件所在列 GridLayout.Spec rowSpec=GridLayout.spec(i/4+2); GridLayout.Spec columnSpec=GridLayout.spec(i%4); GridLayout.LayoutParams params=new GridLayout.LayoutParams(rowSpec, columnSpec); params.setGravity(Gravity.FILL); girdlayout.addView(button, params); } } }
效果如图
智能填充
布局文件
<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:id="@+id/root"android:orientation="vertical" ><AutoCompleteTextView android:id="@+id/auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:completionThreshold="1" /><MultiAutoCompleteTextView android:id="@+id/mauto" android:layout_width="match_parent" android:layout_height="wrap_content" android:completionThreshold="1" /></LinearLayout>代码public class zidongtianchong extends Activity { AutoCompleteTextView act; MultiAutoCompleteTextView mact; String[] omg = new String[] { "1", "12", "123", "1234", }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, omg); act=(AutoCompleteTextView) findViewById(R.id.auto); act.setAdapter(adapter); mact=(MultiAutoCompleteTextView) findViewById(R.id.mauto); mact.setAdapter(adapter); //设置分割符 mact.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); }}
效果如下:
补充:
requestWindowFeatrue(window.Featrue_Progress);
设置标题栏为progressbar;
拖动栏SeekBar的监听方法
0 0
- 疯狂安卓书籍的学习--未完待续~
- BSP RF 相关 安卓底层命令--学习中 未完待续
- 机器学习的那些事儿【未完待续】
- H264的RTP格式学习(未完待续)
- javascript学习笔记(未完待续)
- orcal学习(1)未完待续
- Shell学习笔记...未完待续...
- java并发学习(未完待续)
- jQuery 学习笔记(未完待续)
- 机器学习(未完待续)
- 51学习笔记<未完待续>
- [Oracle]学习Oracle数据库的简单笔记(未完待续)
- Java 设计模式的学习(含代码),未完待续
- 开源项目LookLook的学习笔记(未完待续)
- 我的Altium Designer学习笔记<未完待续>
- [转] 疯狂代码:大型网站架构系列(未完待续)
- 未完待续的几个命令
- 安卓学习书籍推荐
- Nginx和Tengine
- matlab2014a中help与参考页翻译 all
- POJ1523 SPF
- java项目转为web项目
- SDAU dp专题 1002
- 疯狂安卓书籍的学习--未完待续~
- [python]迭代器与生成器
- 如何用jsp+servlet+fliter实现非法字符过滤
- matlab2014a中help与参考页翻译 not
- Unsupported major.minor version 52.0
- Java学习笔记之反射
- Java泛型
- android 多点触摸绘画demo
- untiy 3d ShaderLab_第7章_ Forward渲染路径_2_Forward渲染路径下的重要光源