0%

使用setContentView可以在Activity中动态切换显示的View,这样,不需要多个Activity就可以显示不同的界面,因此不再需要在Activity间传送数据,变量可以直接引用。但是,在android SDK给我们建的默认的Hello World程序中,调用的是setContentView(int layoutResID)方法,如果使用该方法切换view,在切换后再切换回,无法显示切换前修改后的样子,也就是说,相当于重新显示一个view,并非是把原来的view隐藏后再显示。其实setContentView是个多态方法,我们可以先用LayoutInflater把布局xml文件引入成View对象,再通过setContentView(View view)方法来切换视图。因为所有对View的修改都保存在View对象里,所以,当切换回原来的view时,就可以直接显示原来修改后的样子。 下面是代码,main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  android:id="@+id/tv1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="main.xml"
    />
<Button android:id="@+id/b1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="切换到main2.xml"
/>
<Button android:id="@+id/b3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="修改tv1"
/>
</LinearLayout>

main2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  android:id="@+id/tv2"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="main2.xml"
    />
<Button android:id="@+id/b2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="切换到main.xml"
/>
</LinearLayout>

Java 代码:

package com.pocketdigi.changeView;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Main extends Activity {
    /** Called when the activity is first created. */
    TextView tv1, tv2;
    Button b1, b2,b3;
    View layout1, layout2;
    boolean view2Load=false;//main2是否载入过的flag
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //LayoutInflater inflater = getLayoutInflater();
        LayoutInflater inflater = LayoutInflater.from(this);
        //以上两行功能一样
        layout1 = inflater.inflate(R.layout.main, null);
        layout2 = inflater.inflate(R.layout.main2, null);
        setView1();
        tv1 = (TextView) findViewById(R.id.tv1);
        b1 = (Button) findViewById(R.id.b1);
        b3 = (Button) findViewById(R.id.b3);
        b3.setOnClickListener(l3);
        b1.setOnClickListener(l1);
        //控件及监听器只需一次查找绑定,切换view不影响
    }

    private void setView1() {
        setContentView(layout1);
        //切换到main
    }

    private void setView2() {
        setContentView(layout2);
        //切换到main2
        find();
    }
    private void find(){
        if(!view2Load){//如果首次显示main2,查找main2里的控件并绑定监听器
            tv2 = (TextView) findViewById(R.id.tv2);
            b2 = (Button) findViewById(R.id.b2);
            b2.setOnClickListener(l2);
            view2Load=true;//flag设为true
        }
    }

    OnClickListener l1 = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            setView2();
        }
    };
    OnClickListener l2 = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            setView1();
        }

    };
    OnClickListener l3 = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            tv1.setText("已修改");
            //修改tv2的值,切换到main2后再切换回来,可以发现tv1的值被保存了,还是"已修改"
        }

    };
}

在网页中,我们可以用CSS来定义一个样式,然后在HTML中可以重复调用,其实Android中也可以定义样式,重复调用。 定义方法,在strings.xml中的标签前加入以下内容:

    <style name="tvstyle">
        <item name="android:textSize">20sp</item>
        <item name="android:textColor">#ff0000</item>
        <item name="android:text">文本值</item>
    </style>

这里定义一个名为tvstyle的样式,文字大小为20sp,颜色为红色,内容为”文本值” 下面是样式的调用,在布局文件中:

<TextView style="@style/tvstyle"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />

最终效果:

在Android中,如果需要改变控件默认的颜色,包括值的颜色,需要预先在strings.xml中设置,类似字符串,可以反复调用。Android中颜色可以使用drawable或是color来定义。 本例中strings.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Main!</string>
    <string name="app_name">Color</string>
    <drawable name="red">#ff0000</drawable>
    <color name="gray">#999999</color>
    <color name="blue">#0000ff</color>
    <color name="background">#ffffff</color>
</resources>

上面定义了几个颜色值,下面是在布局文件中的调用,main.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background"
    >
<TextView  android:id="@+id/tv1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:textColor="@drawable/red"
    />
<TextView  android:id="@+id/tv2"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:textColor="@color/gray"
    />
<TextView  android:id="@+id/tv3"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>

在Java程序中使用:

package com.pocketdigi.color;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

public class Main extends Activity {
    /** Called when the activity is first created. */
    TextView tv1,tv2,tv3;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv1=(TextView)findViewById(R.id.tv1);
        tv2=(TextView)findViewById(R.id.tv2);
        tv3=(TextView)findViewById(R.id.tv3);
        tv3.setTextColor(Color.BLUE);//直接使用android.graphics.Color的静态变量
        tv2.setTextColor(this.getResources().getColor(R.color.blue));//使用预先设置的颜色值
       
    }
}

这里以TextView为例,其他控件类似.

因为Windows默认把Bios的时间认为是本地时间,而Mac os默认把Bios时间认为是UTC时间,所以,在国内,两个系统显示的时间会相差8小时(北京时间是UTC+8)。知道了问题所在,解决方法就简单了。 我们只需要修改windows注册表,默认将Bios的时间认为是UTC时间就OK了。 在命令提示符(CMD)下输入:Reg add HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation /v RealTimeIsUniversal /t REG_DWORD /d 1,并回车,重启生效。 如果系统是Windows 7或Vista,则找到C:\Windows\System32\cmd.exe 点左键,以管理员身份运行,再输入以上命令修改注册表。

请参考官方快捷键列表: http://support.apple.com/kb/HT1343?viewlocale=zh_CN 如果是USB键盘: Windows键===Command 键图标(Command 键) Ctrl键=====Control 键图标(Control 键) Alt键======Option 或 Alt 键图标(Option 键) 如果是PS2键盘(笔记本自带键盘): Alt键=====Command 键图标(Command 键) Ctrl键=====Control 键图标(Control 键) Windows键======Option 或 Alt 键图标(Option 键) Backspace键=====Delete(仅仅在非文本操作的时候,如果是编辑文本,backspace和delete与windows下效果相同)   常用快捷键: 重命名文件:选中文件或文件夹,按回车 切换输入法:Command+空格(装了QQ输入法后,默认就用QQ输入法吧,五笔模式支持拼音输入,偶尔不会拆的字就拼音,按shift切换中英文) 复制:Command+c 粘贴:Command+v 剪切:Command+x 删除文件或目录:Command+backspace 前往文件夹: Command+Shift+G 新建Finder窗口:Command+n 切换程序窗口:Command+Tab 所有窗口缩到屏幕边(类似Windows下QQ拖到屏幕边自动隐藏的效果,可当作显示桌面功能):F11 显示Dashoard:F12(几个小工具,可以自己添加,默认是计算器,天气,日期,时间) 光标移动到行首:Command+left 光标移动的到行尾:Command+right 光标移动到文件头部:home 光标移动到文件尾部:end 切换桌面:control+left or right 显示Mission Control:control+up(我把右上触发角设为Mission Control,这样鼠标移动到屏幕右上角就可以很方便地切换任务,不需要动键盘) 打开Preference:Command+, 适用任何软件 显示桌面:F11 (不适用全屏模式) 全屏:Control+Command+f,Chrome使用Shift+Command+F会真正全屏,隐藏所有网页内容以外的程序控件包括地址栏(演示模式)。 退出程序:Command+q 强制退出应用程序:Command-Option-Escape

本着能省则省的原则,昨天在淘宝上定了张HASEE的GT220显卡,240大洋,主要是看中了它的低功耗,空载才10W,反正也就开开桌面特效,够了.今天到货.装上机器,发现前天装的mac os 启动不了了,应该是显卡的的问题,重装,顺利启动,显卡驱动用Uinstaller搞定,目前完美1080P,所有驱动完美.硕一白苹果机箱+AOC e2437F+Mac os+白色新贵超薄键盘+白色罗技鼠标,整套山寨苹果花费4000块左右.花费的时间大概在一周左右(新人,从未玩过),当然不是整天都在干这事,如果是Intel平台应该会容易些.

javascript基础学得差不多了,今天正式开始ajax,ajax最关键的当然是与服务器交互。

            var xmlHttp;
            function createXmlHttpRequest(){
                if (window.ActiveXObject) {
                    xmlHttp = new window.ActiveXObject("Microsoft.XMLHTTP");
                }
                else 
                    if (window.XMLHttpRequest) {
                        xmlHttp = new XMLHttpRequest();
                    }
            }
            createXmlHttpRequest();
            function GET(){
                xmlHttp.open("GET", "3.php", true);
                xmlHttp.onreadystatechange = function(){
                    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                        alert(xmlHttp.responseText);
                    }
                }
                xmlHttp.send(null);
            }
            function POST(){
                xmlHttp.open("POST","3.php");
                alert("POST");
                xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                xmlHttp.onreadystatechange = function(){
                    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                        alert(xmlHttp.responseText);
                    }
                }
                xmlHttp.send("name=zhang");
            }

经过几天的研究,折腾,总算是把mac os安装在了amd平台上,前前后后至少装了十来次吧,硬件配置见 第一次DIY台式机,现在唯一的问题是集成显卡hd4290没有驱动,我也不打算再折腾了,准备直接花几百块买张能用的.mac os做得确实不错,只是不太习惯键盘,普通的windows键盘有些键是不同的.比如原来的ctrl+c ctrl+v变成了win+c win+v,确实需要点时间适应.

下面的例子是通过监听点击事件,动态添加行,删除行、单元格。

        无标题文档
        
            function add(){
                var tr=document.getElementById("table").insertRow(0);//insertRow参数为插入后的行号,或者说插到这一行前面
                var data=new Array();//定义数组,数据放里面,每格创建一个TEXT节点
                data[0]=document.createTextNode("1");
                data[1]=document.createTextNode("2");
                data[2]=document.createTextNode("3");
                for(i=0;i<data.length;i++){//循环插入
                    td=tr.insertCell(i);//insertCell参数为列号
                    td.appendChild(data[i]);//绑定
                }
            }
            function remove(){
                var table=document.getElementById("table");
                table.deleteRow(0);//移除行,下面补齐
                table.rows[0].deleteCell(0);//移除单元格,后面的自动上前补齐
            }
        
    
    
        增加一行  删除一行
        
            
                
                    asdf
                
                
                    asfd
                
                
                    sfda
                
            
            
                
                    asdf
                
                
                    asfd
                
                
                    sfda
                
            
            
                
                    asdf
                
                
                    asfd
                
                
                    sfda

IE是个奇怪的浏览器,背离国际标准,有一套自己的标准,导致开发者必须对各种浏览器进行兼容性测试。但是因为微软的捆绑销售,市场占有率非常高,而在国内,因为Ghost XP的盛行,IE6.0这个连微软自己都想K掉的版本占有率却达到60%以上。 下面是代码,点击以后会在点我后面加上点了两字,兼容Firefox,IE

        无标题文档
        
            function add(){
                var a=document.getElementById("p");
                a.innerHTML+="点了";
                if(window.event){
                    p.detachEvent("onclick",add);//删除IE监听函数
                }else{
                    p.removeEventListener("click",add,false);//删除Firefox监听函数
                }		
            }
            var p;
            window.onload=function(){
                p=document.getElementById("p");
                if(window.event){// 如果是IE,用attachEvent绑定onclick事件,执行add函数
                p.attachEvent("onclick",add);
                }else{//否则用addEventListener绑定
                p.addEventListener("click",add,false);//false表示为冒泡型,非捕获型
                }
            }
        
    
    
        
            点我