ViewTreeObserver示例(二)

当前位置 : 首页 > 网页制作 > CSS > ViewTreeObserver示例(二)

ViewTreeObserver示例(二)

来源: 作者: 时间:2016-02-16 10:04
main.xml如下:[html] LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:id=@+id/LinearLayout android:layout_width=fill_parent android:layout_height=fill_pare...

main.xml如下:

[] <LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
   > 
 
    <TextView 
        android:id="@+id/touchModeChangeTextView" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello_world"  
        android:layout_marginTop="30dip" 
        android:gravity="center" 
        android:textColor="@android:color/black" 
        android:textSize="25sp" 
        /> 
     
    <EditText 
        android:id="@+id/firstEditText" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="30dip" 
        android:gravity="center" 
        android:textColor="@android:color/black" 
        android:textSize="25sp" 
        /> 
     
    <EditText 
        android:id="@+id/secondEditText" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="30dip" 
        android:gravity="center" 
        android:textColor="@android:color/black" 
        android:textSize="25sp" 
        /> 
    <TextView 
        android:id="@+id/globalFocusChangeTextView" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:hint="显示焦点的变化" 
        android:layout_marginTop="30dip" 
        android:gravity="center" 
        android:textColor="@android:color/black" 
        android:textSize="15sp" 
      /> 
      
     
    <Button 
        android:id="@+id/button" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="30dip" 
        android:gravity="center" 
        android:text="切换第二个EditText的显示和隐藏" 
        android:textColor="@android:color/black" 
        android:textSize="15sp" 
      /> 
     
     
</LinearLayout> 

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
   >

    <TextView
        android:id="@+id/touchModeChangeTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:layout_marginTop="30dip"
        android:gravity="center"
        android:textColor="@android:color/black"
        android:textSize="25sp"
        />
   
    <EditText
        android:id="@+id/firstEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dip"
        android:gravity="center"
        android:textColor="@android:color/black"
        android:textSize="25sp"
        />
   
    <EditText
        android:id="@+id/secondEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dip"
        android:gravity="center"
        android:textColor="@android:color/black"
        android:textSize="25sp"
        />
    <TextView
        android:id="@+id/globalFocusChangeTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="显示焦点的变化"
        android:layout_marginTop="30dip"
        android:gravity="center"
        android:textColor="@android:color/black"
        android:textSize="15sp"
      />
    
   
    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dip"
        android:gravity="center"
        android:text="切换第二个EditText的显示和隐藏"
        android:textColor="@android:color/black"
        android:textSize="15sp"
      />
   
   
</LinearLayout>
 

MainActivity如下:

[java]  package cn.com.testviewtreeobserver2; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewTreeObserver; 
import android.view.ViewTreeObserver.OnGlobalFocusChangeListener; 
import android.view.ViewTreeObserver.OnGlobalLayoutListener; 
import android.view.ViewTreeObserver.OnPreDrawListener; 
import android.view.ViewTreeObserver.OnTouchModeChangeListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.app.Activity; 
/**
 * Demo描述:
 * ViewTreeObserver的测试
 * 备注说明:
 * OnTouchModeChangeListener的相关
 * 测试未实现
 * 重要参考:
 * http://blog.csdn.net/pathuang68/article/details/6431035
 * Thank you
 *
 */ 
public class MainActivity extends Activity { 
    private View mLinearLayout; 
    private TextView mTouchModeChangeTextView; 
    private EditText mFirstEditText; 
    private EditText mSecondEditText; 
    private TextView mGlobalFocusChangeTextView; 
    private Button mButton; 
    private ViewTreeObserver mViewTreeObserver; 
    private boolean isClick=false; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        init(); 
    } 
    private void init(){ 
        mLinearLayout=findViewById(R.id.LinearLayout); 
        mTouchModeChangeTextView=(TextView) findViewById(R.id.touchModeChangeTextView); 
        mFirstEditText=(EditText) findViewById(R.id.firstEditText); 
        mSecondEditText=(EditText) findViewById(R.id.secondEditText); 
        mGlobalFocusChangeTextView=(TextView) findViewById(R.id.globalFocusChangeTextView); 
        mButton=(Button) findViewById(R.id.button); 
        mButton.setOnClickListener(new ButtonOnClickListenerImpl()); 
        mViewTreeObserver=mLinearLayout.getViewTreeObserver(); 
        viewTreeObserverAddListeners(); 
    } 
    //为ViewTreeObserver设置各种监听  
    private void viewTreeObserverAddListeners(){ 
        //当一个视图树的触摸模式发生改变时,调用该方法  
        //在该实例中未实现对于该方法的相关测试  
        mViewTreeObserver.addOnTouchModeChangeListener(new OnTouchModeChangeListener() { 
            @Override 
            public void onTouchModeChanged(boolean isInTouchMode) { 
                if (isInTouchMode) { 
                    mTouchModeChangeTextView.setText("In touch mode"); 
                }else{ 
                    mTouchModeChangeTextView.setText("Not in touch mode"); 
                } 
            } 
        }); 
        //当在一个视图树中全局布局发生改变  
        //或者视图树中的某个视图的可视状态发生改变时调用该方法  
        mViewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
            @Override 
            public void onGlobalLayout() { 
                if (isClick) { 
                    if (mSecondEditText.isShown()) { 
                        mFirstEditText.setText("监听到第二个TextView被显示"); 
                    } else { 
                        mFirstEditText.setText("监听到第二个TextView被隐藏"); 
                    } 
                } 
            } 
        }); 
        //当一个视图树将要绘制时,调用该方法  
        //此处当mFirstEditText将要绘制时给其设置了hint和字体大小  
        //注意:必须返回true,否则整个界面不能完整显示  
        mViewTreeObserver.addOnPreDrawListener(new OnPreDrawListener() { 
            @Override 
            public boolean onPreDraw() { 
                mFirstEditText.setHint("调用了OnPreDrawListener"); 
                mFirstEditText.setTextSize((float)15.0); 
                return true; 
            } 
        }); 
        //当在一个视图树中的焦点状态发生改变时,调用该方法  
        mViewTreeObserver.addOnGlobalFocusChangeListener(new OnGlobalFocusChangeListener() { 
            @Override 
            public void onGlobalFocusChanged(View oldFocus, View newFocus) { 
                if (oldFocus!=null&&newFocus!=null) { 
                    mGlobalFocusChangeTextView.setText 
                    ("Focus FROM:" + oldFocus.toString()+ " TO:" + newFocus.toString()); 
                } 
            } 
        }); 
    } 
     
    //注意使用View.INVISIBLE不会调用OnGlobalLayoutListener  
    //因为View.INVISIBLE只是控件不可见却还占据着位置,此时  
    //布局在实质上没有发生变化  
    private class ButtonOnClickListenerImpl implements OnClickListener{ 
        @Override 
        public void onClick(View v) { 
            mLinearLayout.setClickable(false); 
            isClick=true; 
            if (mSecondEditText.isShown()) { 
                mSecondEditText.setVisibility(View.GONE); 
            } else { 
                mSecondEditText.setVisibility(View.VISIBLE); 
            } 
        } 
    } 

package cn.com.testviewtreeobserver2;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalFocusChangeListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.view.ViewTreeObserver.OnTouchModeChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
/**
 * Demo描述:
 * ViewTreeObserver的测试
 * 备注说明:
 * OnTouchModeChangeListener的相关
 * 测试未实现
 * 重要参考:
 * http://blog.csdn.net/pathuang68/article/details/6431035
 * Thank you
 *
 */
public class MainActivity extends Activity {
 private View mLinearLayout;
 private TextView mTouchModeChangeTextView;
 private EditText mFirstEditText;
 private EditText mSecondEditText;
 private TextView mGlobalFocusChangeTextView;
 private Button mButton;
 private ViewTreeObserver mViewTreeObserver;
 private boolean isClick=false;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  init();
 }
    private void init(){
     mLinearLayout=findViewById(R.id.LinearLayout);
     mTouchModeChangeTextView=(TextView) findViewById(R.id.touchModeChangeTextView);
     mFirstEditText=(EditText) findViewById(R.id.firstEditText);
     mSecondEditText=(EditText) findViewById(R.id.secondEditText);
     mGlobalFocusChangeTextView=(TextView) findViewById(R.id.globalFocusChangeTextView);
     mButton=(Button) findViewById(R.id.button);
     mButton.setOnClickListener(new ButtonOnClickListenerImpl());
     mViewTreeObserver=mLinearLayout.getViewTreeObserver();
     viewTreeObserverAddListeners();
    }
    //为ViewTreeObserver设置各种监听
    private void viewTreeObserverAddListeners(){
     //当一个视图树的触摸模式发生改变时,调用该方法
     //在该实例中未实现对于该方法的相关测试
     mViewTreeObserver.addOnTouchModeChangeListener(new OnTouchModeChangeListener() {
   @Override
   public void onTouchModeChanged(boolean isInTouchMode) {
    if (isInTouchMode) {
     mTouchModeChangeTextView.setText("In touch mode");
    }else{
     mTouchModeChangeTextView.setText("Not in touch mode");
    }
   }
  });
     //当在一个视图树中全局布局发生改变
     //或者视图树中的某个视图的可视状态发生改变时调用该方法
     mViewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
   @Override
   public void onGlobalLayout() {
    if (isClick) {
     if (mSecondEditText.isShown()) {
      mFirstEditText.setText("监听到第二个TextView被显示");
     } else {
      mFirstEditText.setText("监听到第二个TextView被隐藏");
     }
    }
   }
  });
     //当一个视图树将要绘制时,调用该方法
     //此处当mFirstEditText将要绘制时给其设置了hint和字体大小
     //注意:必须返回true,否则整个界面不能完整显示
     mViewTreeObserver.addOnPreDrawListener(new OnPreDrawListener() {
   @Override
   public boolean onPreDraw() {
    mFirstEditText.setHint("调用了OnPreDrawListener");
    mFirstEditText.setTextSize((float)15.0);
    return true;
   }
  });
     //当在一个视图树中的焦点状态发生改变时,调用该方法
     mViewTreeObserver.addOnGlobalFocusChangeListener(new OnGlobalFocusChangeListener() {
   @Override
   public void onGlobalFocusChanged(View oldFocus, View newFocus) {
    if (oldFocus!=null&&newFocus!=null) {
     mGlobalFocusChangeTextView.setText
     ("Focus FROM:" + oldFocus.toString()+ " TO:" + newFocus.toString());
    }
   }
  });
    }
   
    //注意使用View.INVISIBLE不会调用OnGlobalLayoutListener
 //因为View.INVISIBLE只是控件不可见却还占据着位置,此时
 //布局在实质上没有发生变化
    private class ButtonOnClickListenerImpl implements OnClickListener{
  @Override
  public void onClick(View v) {
   mLinearLayout.setClickable(false);
   isClick=true;
   if (mSecondEditText.isShown()) {
    mSecondEditText.setVisibility(View.GONE);
   } else {
    mSecondEditText.setVisibility(View.VISIBLE);
   }
  }
    }
}


 

Tag:
网友评论

<