android ScrollView(捲軸視圖)

來源:互聯網
上載者:User

ScrollView01.java:

 

package com.ScrollView01;

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 ScrollView01 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);
        mScrollView=(ScrollView)findViewById(R.id.ScrollView01);
       
        Button button=(Button)findViewById(R.id.Button01);
       
        button.setOnClickListener(mClickListener);
        //改變預設焦點切換
        button.setOnKeyListener(mAddButtonKeyListener);
    }
   
    //當點擊按鈕時,增加一個TextView和Button
    private Button.OnClickListener mClickListener=new Button.OnClickListener()
    {
     private int mIndex=1;
     @Override
     public void onClick(View arg0)
     {
      TextView mTextView=new TextView(ScrollView01.this);
      mTextView.setText("Text View"+mIndex);
      LinearLayout.LayoutParams p=new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.FILL_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT
        );
      //增加一個TextView到線性布局中
      mLayout.addView(mTextView,p);
      
      Button mButtonView=new Button(ScrollView01.this);
      mButtonView.setText("Button"+mIndex++);
      mLayout.addView(mButtonView,p);
      //改變預設焦點切換
      mButtonView.setOnKeyListener(mNewButtonKeyListener);
      //投遞一個訊息進行滾動
      mHandler.post(mScrollToBottom);
     }    
    };
    

    //建立後滾動到最後
    private Runnable mScrollToBottom=new Runnable()
    {
     public void run()
     {
      int off=mLayout.getMeasuredHeight()-mScrollView.getHeight();
      if(off>0)
      {
       mScrollView.scrollTo(0, off);
      }
     }
    };
   

    //滾動到最後回到頭
    private View.OnKeyListener mNewButtonKeyListener=new View.OnKeyListener()
    { 
  @Override
  public boolean onKey(View v, int keyCode, KeyEvent event)
  {
   if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN&&
     v==mLayout.getChildAt(mLayout.getChildCount()-1))
   {
    findViewById(R.id.Button01).requestFocus();
    return true;
   }
   // TODO Auto-generated method stub
   return false;
  }
 };
   

     //滾動到頭回到尾
    private View.OnKeyListener mAddButtonKeyListener=new Button.OnKeyListener()
    {
     @Override
     public boolean onKey(View v,int keyCode,KeyEvent event)
     {
      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;
      }
     }
    };
}

 

 

main.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<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="Button"/>
</LinearLayout>
</ScrollView>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.