DDMS視圖和Button

來源:互聯網
上載者:User

今天學了在Eclipse中使用DDMS視圖,在這個視圖中最重要的是LogCat的使用。。。。

1   DDMS視圖和Button

1.1  Button

<Button

       android:id="@+id/btn1"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:text="點擊我"

   />

1.2    給Button添加事件

public class MainActivityextends Activity {

   private Button btn1 =null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        btn1=(Button)findViewById(R.id.btn1);

        btn1.setOnClickListener(new OnClickListener()

     {

       

        @Override

        public void onClick(View v)

        {

          Toast.makeText(MainActivity.this,"你點擊了按鈕", Toast.LENGTH_LONG).show();

         

        }

     });

    }

}

1.3  執行個體:

在介面上添加兩個按鈕,點擊第一個按鈕現實提示資訊,點擊第二個按鈕在日誌LogCat中輸出資訊。代碼如下:

在main.xml檔案中,

<Button

    android:id="@+id/btn1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="點擊我1"

    />

     <Button

    android:id="@+id/btn2"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="點擊我2"

    />

在MainActivity.java檔案中:

packagecom.jsd.button.activity;

 

import android.app.Activity;

importandroid.inputmethodservice.Keyboard.Key;

import android.os.Bundle;

import android.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

import android.widget.Toast;

 

public class MainActivityextends Activity {

   private Button btn1 = null;

   private Button btn2 = null;

    @Override

    public void onCreate(BundlesavedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        btn1=(Button)findViewById(R.id.btn1);

        btn2=(Button)findViewById(R.id.btn2);

        btn1.setOnClickListener(listener);

        btn2.setOnClickListener(listener);

    }

    private OnClickListener listener=newOnClickListener()

   {

     

      @Override

      public void onClick(View v)

      {

        Button btn=(Button)v;

        switch (btn.getId())

        {

        case R.id.btn1:

           Toast.makeText(MainActivity.this,"你點擊了按鈕",Toast.LENGTH_LONG).show();

           break;

        case R.id.btn2:

           System.out.println("你點擊了button2");

           break;

          

        }

      }

   };

}

運行效果如下:

 

                                     

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.