Android学习笔记1

 
 //AlertDialog对话框的创建和使用,
  布局文件中添加一个button,绑定了onClick事件
     <Button
       android:text="一般的AlertDialog"
       android:id="@+id/normal"
         android:layout_width="fill_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
         android:onClick="normalClick"
        />

   //直接在xml文件里绑定了方法
    public void normalClick(View v) {//view是按钮对象
        //AlertDialog.Builder的create()方法和show()方法都能创建一个AlertDialog,
        //setPositiveButton或者setNegativeButton是AlertDialog.Builder的方法不是AlertDialog的
       //而且AlertDialog没有setPositiveButton等几个按钮的设置方法
         //可以直接设置button
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
         alert.setIcon(R.drawable.ic_launcher);
       alert.setTitle("提示信息");
         alert.setMessage("尝试点击确定和取消按钮");
         alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
             public void onClick(DialogInterface dialog, int which) {
               Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT).show();
             }
        });
        alert.setNeutralButton("帮助", new DialogInterface.OnClickListener() {
             @Override
           public void onClick(DialogInterface dialog, int which) {
              Toast.makeText(MainActivity.this, "点击了帮助按钮", Toast.LENGTH_SHORT).show();
            }
         });
       alert.setNegativeButton("取消", new DialogInterface.OnClickListener() {
           @Override
             public void onClick(DialogInterface dialog, int which) {
               Toast.makeText(MainActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();
            }
         });
        //alert.create();
         //这里可以简单的写,因为show()方法的内部就有create
       alert.show();
     }
 1 //使用代码链的方式,创建AlertDialog
 2 //布局文件
 3     <Button
 4         android:text="列表对话框"
 5         android:id="@+id/list"
 6         android:layout_width="fill_parent"
 7         android:layout_height="0dp"
 8         android:layout_weight="1"
 9         />
10 //
11 public class MainActivity extends Activity implements View.OnClickListener {
12 
13   @Override
14     protected void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.activity_main);
17         //获取按钮对象
18         Button btn = (Button) findViewById(R.id.list);
19         //设置监听,参数是一个实现了View.OnClickListener接口的对象
20         btn.setOnClickListener(this);
21        
22     }
23 
24  @Override
25     public void onClick(View v) {
26         //实现一个列表对话框
27         //使用代码链的写法创建AlertDialog,不实例对象
28         //调用setXXX();方法返回的都是一个AlertDialog.Builder对象
29         //使用setItems来创建一个列表对话框,两个参数
30         //第一个是每个item显示的数据,是一个数组。第二个参数是点击触发的方法
31         final String[] arr = {"选项1", "选项2", "选项3", "选项4", "选项5", "选项6", "选项7"};
32         new AlertDialog.Builder(this)
33                 .setTitle("列表对话框")
34                 .setItems(arr, new DialogInterface.OnClickListener() {
35                     @Override
36                     public void onClick(DialogInterface dialog, int which) {
37                         //which是当前点击的item在数据数组中的下标
38                         //我们如果老用到Toast和Log.i 日志输出什么的可以把他单独弄一个类,这里是创建了Utils类创建了一个静态方法print
39                         //添加final是跨域的问题,如果不设置final数组的生命周期只到方法结束,当点击的时候,数组销毁了,拿不到值,所以把他保存到常量池中,可以声明一个全局变量
40                         Utils.print(MainActivity.this, arr[which] + "");
41                     }
42                 })
43                 .show();
44     //可以先把show()方法写上,代码链比较长容易忘
45     }
46 
47 }

 使用java代码的方式来实现渐变动画

 

 1 public class javaAlpha_Activity extends Activity {
 2 
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_java_alpha_);
 7         ImageView img = (ImageView) findViewById(R.id.img_Alpha_java);
 8         //使用java代码实现也非常简单,直接设置一个Animation类的对象就可以了
 9         //AlphaAnimation 是Animation的子类
10         //在构造函数中设置他的初始状态fromAlpha和最终状态toAlpha,是float类型
11         Animation animation = new AlphaAnimation(0.1f,1.0f);
12         //设置持续时间
13         animation.setDuration(2000);
14         //设置重复次数
15         animation.setRepeatCount(3);
16         //清除原有的动画,避免多次点击出现重复的效果
17         img.clearAnimation();
18         //开始执行动画
19         img.startAnimation(animation);
20     }
21 }

 

 

 


posted on 2016-02-23 22:04  Become_stronger  阅读(175)  评论(0)    收藏  举报

导航