美文网首页
安卓第二课

安卓第二课

作者: 我是上帝可爱多 | 来源:发表于2017-09-04 16:20 被阅读23次
    1 吐司制作
    public void makeToast(String str, int showTime){
        Toast toast = Toast.makeText(global_context, str, showTime);            
        toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL , 0, 0);  //设置显示位置
        TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
        v.setTextColor(Color.YELLOW);     //设置字体颜色
        toast.show();   
    }
    

    下面来一个高级的吐司

    private void midToast(String str, int showTime)
    {
        Toast toast = Toast.makeText(mContext, str, showTime);
        toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM , 0, 0);  //设置显示位置
        LinearLayout layout = (LinearLayout) toast.getView();
        layout.setBackgroundColor(Color.BLUE);
        ImageView image = new ImageView(this);
        image.setImageResource(R.mipmap.ic_icon_qitao);
        layout.addView(image, 0);
        TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
        v.setTextColor(Color.YELLOW);     //设置字体颜色
        toast.show();
    }
    

    完全自定义toast

    private void midToast(String str, int showTime)
    {
        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.view_toast_custom,
                (ViewGroup) findViewById(R.id.lly_toast));
        ImageView img_logo = (ImageView) view.findViewById(R.id.img_logo);
        TextView tv_msg = (TextView) view.findViewById(R.id.tv_msg);
        tv_msg.setText(str);
        Toast toast = new Toast(mContext);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(view);
        toast.show();
    }
    
    圆角背景:bg_toast.xml:
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- 设置透明背景色 -->
        <solid android:color="#BADB66" />
        <!-- 设置一个黑色边框 -->
        <stroke
            android:width="1px"
            android:color="#FFFFFF" />
        <!-- 设置四个圆角的半径 -->
        <corners
            android:bottomLeftRadius="50px"
            android:bottomRightRadius="50px"
            android:topLeftRadius="50px"
            android:topRightRadius="50px" />
        <!-- 设置一下边距,让空间大一点 -->
        <padding
            android:bottom="5dp"
            android:left="5dp"
            android:right="5dp"
            android:top="5dp" />
    </shape>  
    
    view_toast_custom.xml:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/lly_toast"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_toast"
        android:orientation="horizontal">
    
        <ImageView
            android:id="@+id/img_logo"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginLeft="10dp"
            android:src="@mipmap/iv_lol_icon1" />
    
        <TextView
            android:id="@+id/tv_msg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:textSize="20sp" />
    
    </LinearLayout>
    

    2.scrollView

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        private Button btn_down;
        private Button btn_up;
        private ScrollView scrollView;
        private TextView txt_show;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            bindViews();
        }
    
    
        private void bindViews() {
            btn_down = (Button) findViewById(R.id.btn_down);
            btn_up = (Button) findViewById(R.id.btn_up);
            scrollView = (ScrollView) findViewById(R.id.scrollView);
            txt_show = (TextView) findViewById(R.id.txt_show);
            btn_down.setOnClickListener(this);
            btn_up.setOnClickListener(this);
    
            StringBuilder sb = new StringBuilder();
            for (int i = 1; i <= 100; i++) {
                sb.append("呵呵 * " + i + "\n");
            }
            txt_show.setText(sb.toString());
    
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_down:
                    scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                    break;
                case R.id.btn_up:
                    scrollView.fullScroll(ScrollView.FOCUS_UP);
                    break;
            }
        }
    }
    
    3 处理事件
    public class MainActivity extends Activity {    
        private Button btnshow;    
            
        @Override    
        protected void onCreate(Bundle savedInstanceState) {    
            super.onCreate(savedInstanceState);    
            setContentView(R.layout.activity_main);    
            btnshow = (Button) findViewById(R.id.btnshow);    
            btnshow.setOnClickListener(new OnClickListener() {    
                //重写点击事件的处理方法onClick()    
                @Override    
                public void onClick(View v) {    
                    //显示Toast信息    
                    Toast.makeText(getApplicationContext(), "你点击了按钮", Toast.LENGTH_SHORT).show();    
                }    
            });    
        }        
    } 
    

    在我看来下面这个是野路子

    public class MainActivity extends Activity {    
        @Override    
        protected void onCreate(Bundle savedInstanceState) {    
            super.onCreate(savedInstanceState);    
            setContentView(R.layout.activity_main);     
        }    
        //自定义一个方法,传入一个view组件作为参数    
        public void myclick(View source)    
        {    
            Toast.makeText(getApplicationContext(), "按钮被点击了", Toast.LENGTH_SHORT).show();    
        }    
    } 
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
        xmlns:tools="http://schemas.android.com/tools"    
        android:id="@+id/LinearLayout1"    
        android:layout_width="match_parent"    
        android:layout_height="match_parent"    
        android:orientation="vertical" >    
        <Button     
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="按钮"    
            android:onClick="myclick"/>    
     </LinearLayout> 
    
    4 Handler
    public void onReflash() {
            // TODO Auto-generated method stub\
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                
                @Override
                public void run() {
                    setReflashData();
                    showList(apk_list);
                    listview.reflashComplete();
                }
            }, 2000);
        }
    

    不说了,这不就是js的settimeout

    public class MainActivity extends Activity {  
      
        //定义切换的图片的数组id  
        int imgids[] = new int[]{  
            R.drawable.s_1, R.drawable.s_2,R.drawable.s_3,  
            R.drawable.s_4,R.drawable.s_5,R.drawable.s_6,  
            R.drawable.s_7,R.drawable.s_8  
        };  
        int imgstart = 0;  
          
        final Handler myHandler = new Handler()  
        {  
          @Override  
          //重写handleMessage方法,根据msg中what的值判断是否执行后续操作  
          public void handleMessage(Message msg) {  
            if(msg.what == 0x123)  
               {  
                imgchange.setImageResource(imgids[imgstart++ % 8]);  
               }  
            }  
        };  
        
          
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            final ImageView imgchange = (ImageView) findViewById(R.id.imgchange);  
             
            //使用定时器,每隔200毫秒让handler发送一个空信息  
            new Timer().schedule(new TimerTask() {            
                @Override  
                public void run() {  
                    myHandler.sendEmptyMessage(0x123);  
                      
                }  
            }, 0,200);  
        }  
      
    } 
    
    5 ViewPager
    public class MyPagerAdapter extends PagerAdapter {
        private ArrayList<View> viewLists;
    
        public MyPagerAdapter() {
        }
    
        public MyPagerAdapter(ArrayList<View> viewLists) {
            super();
            this.viewLists = viewLists;
        }
    
        @Override
        public int getCount() {
            return viewLists.size();
        }
    
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewLists.get(position));
            return viewLists.get(position);
        }
    
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(viewLists.get(position));
        }
    }
    
    public class OneActivity extends AppCompatActivity{
    
        private ViewPager vpager_one;
        private ArrayList<View> aList;
        private MyPagerAdapter mAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_one);
            vpager_one = (ViewPager) findViewById(R.id.vpager_one);
    
            aList = new ArrayList<View>();
            LayoutInflater li = getLayoutInflater();
            aList.add(li.inflate(R.layout.view_one,null,false));
            aList.add(li.inflate(R.layout.view_two,null,false));
            aList.add(li.inflate(R.layout.view_three,null,false));
            mAdapter = new MyPagerAdapter(aList);
            vpager_one.setAdapter(mAdapter);
        }
    }
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="#FFC3AB"
            android:gravity="center"
            android:text="简单的ViewPager使用"
            android:textSize="18sp"
            android:textColor="#000000"
            android:textStyle="bold" />
    
    
        <android.support.v4.view.ViewPager
            android:id="@+id/vpager_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />
    </LinearLayout>  
    

    相关文章

      网友评论

          本文标题:安卓第二课

          本文链接:https://www.haomeiwen.com/subject/qzlejxtx.html