• Android-ScrollView滚动试图Demo


    代码

    package com.lxt008;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.KeyEvent;
    import android.view.View;
    import android.widget.Button;
    import android.widget.LinearLayout;
    import android.widget.ScrollView;
    import android.widget.TextView;
    
    public class Activity01 extends Activity
    {
        private LinearLayout mLayout;   
        private ScrollView     mScrollView;   
        private final Handler mHandler = new Handler();  
        
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            //创建一个线性布局
            mLayout = (LinearLayout) findViewById(R.id.layout);   
            //创建一个ScrollView对象
            mScrollView = (ScrollView) findViewById(R.id.ScrollView01);   
      
            Button button = (Button) findViewById(R.id.Button01); 
            
            button.setOnClickListener(mClickListener);   
            //改变默认焦点切换   
            button.setOnKeyListener(mAddButtonKeyListener);
        }
        
        //Button事件监听
        //当点击按钮时,增加一个TextView和Button
        private Button.OnClickListener mClickListener = new Button.OnClickListener() 
        {   
            private int mIndex = 1;   
            @Override  
            public void onClick(View arg0) 
            {   
                // TODO Auto-generated method stub        
                TextView textView = new TextView(Activity01.this);   
                textView.setText("Text View " + mIndex);   
                LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(   
                        LinearLayout.LayoutParams.FILL_PARENT,   
                        LinearLayout.LayoutParams.WRAP_CONTENT   
                );   
                //增加一个TextView到线性布局中
                mLayout.addView(textView, p);   
      
                Button buttonView = new Button(Activity01.this);   
                buttonView.setText("Button " + mIndex++);
                
              //增加一个Button到线性布局中
                mLayout.addView(buttonView, p);   
                //改变默认焦点切换  
                buttonView.setOnKeyListener(mNewButtonKeyListener); 
                //投递一个消息进行滚动   
                mHandler.post(mScrollToBottom);   
            }          
        };   
        
        
        private Runnable mScrollToBottom = new Runnable() 
        {   
            @Override  
            public void run()
            {   
                // TODO Auto-generated method stub   
                
                int off = mLayout.getMeasuredHeight() - mScrollView.getHeight();   
                if (off > 0) 
                {   
                    mScrollView.scrollTo(0, off);   
                }                          
            }   
        };   
        
        //事件监听
        private View.OnKeyListener mNewButtonKeyListener = new View.OnKeyListener() 
        {   
            public boolean onKey(View v, int keyCode, KeyEvent event) 
            {   
                if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN &&   
                        event.getAction() == KeyEvent.ACTION_DOWN &&   
                        v == mLayout.getChildAt(mLayout.getChildCount() - 1)) 
                {   
                    findViewById(R.id.Button01).requestFocus();   
                    return true;   
                }   
                return false;   
            }   
        };   
        
      //事件监听
        private View.OnKeyListener mAddButtonKeyListener = new Button.OnKeyListener() 
        {   
            @Override  
            public boolean onKey(View v, int keyCode, KeyEvent event) 
            {   
                // TODO Auto-generated method stub                  
                View viewToFoucus = null;   
                if (event.getAction() == KeyEvent.ACTION_DOWN) 
                {   
                    int iCount = mLayout.getChildCount(); 
                    switch (keyCode) 
                    {   
                    case KeyEvent.KEYCODE_DPAD_UP:   
                        if ( iCount > 0) 
                        {   
                            viewToFoucus = mLayout.getChildAt(iCount - 1);   
                        }   
                        break;   
                    case KeyEvent.KEYCODE_DPAD_DOWN:   
                        if (iCount < mLayout.getWeightSum()) 
                        {   
                            viewToFoucus = mLayout.getChildAt(iCount + 1);   
                        }   
                        break;   
                    default:   
                        break;   
                    }   
                }     
                if (viewToFoucus != null) 
                {   
                    viewToFoucus.requestFocus();   
                    return true;   
                } 
                else 
                {   
                    return false;   
                }   
            }   
        }; 
    }

    布局文件

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ScrollView01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none">
        <LinearLayout
            android:id="@+id/layout"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="TextView0"/>
    
            <Button
               android:id="@+id/Button01"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Button0"/>
        </LinearLayout>
    </ScrollView>
  • 相关阅读:
    BestCoder Round #71 (div.2) (hdu 5621)
    BestCoder Round #71 (div.2) (hdu 5620 菲波那切数列变形)
    BestCoder Round #69 (div.2) Baby Ming and Weight lifting(hdu 5610)
    BestCoder Round #70 Jam's math problem(hdu 5615)
    BestCoder Round #68 (div.2) tree(hdu 5606)
    poj 2299 Ultra-QuickSort(归并排序或是bit 树+离散化皆可)
    hdu 3874 Necklace(bit树+事先对查询区间右端点排序)
    HDU 4300 Clairewd’s message(KMP)
    HDU 3308 LCIS(线段树单点更新区间合并)
    PKU 3667 Hotel(线段树)
  • 原文地址:https://www.cnblogs.com/spadd/p/4189880.html
Copyright © 2020-2023  润新知