IT尋寶
          程序員的世界
          posts - 0,  comments - 2,  trackbacks - 0
          現在介紹按鈕響應的兩種方法:

            方法一:

             在onCreate中添加如下代碼
            Button btn = (Button)findViewById(R.id.button);
            Button btn2 = (Button)findViewById(R.id.button2);
            btn.setonClickListener(this);
            btn2.setonClickListener(this);

            另外有一個onClick函數如下:
            public void onClick(View arg0) {
            switch (arg0.getId()) {
            case R.id.button: setTitle("this is OK button"); break;
            case R.id.button2: this.finish(); break;
            }
            }

            這里的arg0作為響應參數,選擇觸發了哪個控件。當然,該程序需要在main.xml中加入兩個按鈕的說明:
            < Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="OK"/ >
            < Button android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Exit"/ >
            還需要庫: import android.view.View.onClickListener;

            方法二:

            在onCreate中添加如下代碼
            btn.setonClickListener(listener);
            btn2.setonClickListener(listener2);
            然后添加兩個 onClickListener:
            onClickListener listener = new onClickListener() {
            public void onClick(View v) {
            setTitle("this is OK button");
            }
            };
            onClickListener listener2 = new onClickListener() {
            public void onClick(View v) {
            finish();
              }
          posted on 2011-06-15 17:07 特務小強 閱讀(727) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          留言簿

          隨筆分類

          文章檔案

          搜索

          •  

          最新評論

          主站蜘蛛池模板: 新邵县| 章丘市| 饶阳县| 罗源县| 磴口县| 鄯善县| 贵溪市| 石柱| 梅州市| 永年县| 蓬溪县| 镶黄旗| 珲春市| 临湘市| 唐河县| 新乡县| 沙洋县| 宣城市| 大港区| 古交市| 竹北市| 游戏| 阿克| 齐河县| 青冈县| 贵南县| 广元市| 台东县| 化州市| 平陆县| 桑植县| 科技| 高要市| 左贡县| 德化县| 屯昌县| 都安| 轮台县| 秦安县| 拜泉县| 阳高县|