Android響應按鈕的程式結構

來源:互聯網
上載者:User

 

  圖形介面的按鈕是最普通的控制項,Android響應按鈕的程式結構大體分成如下兩種:

    (1)採樣匿名內部類的按鈕實現方式:

    如下面的Java程式,在聲明Class時只是從Activity派生,並沒有實現任何介面。因此為了實現按鈕事件的響應,就必須在下面的方法中用匿名內部類的方式實現,比如

       //退出按鈕;

        bt_exit.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                // Perform action on click

             finish();

            }

        });

在這段程式中,new View.OnClickListener() 就是匿名的的內部類,而

public void onClick(View v) {

                // Perform action on click

             finish();

            }

就是這個內部類onClick方法的實現。

    上述完整的程式結構如下:

public class a15act extends Activity {

  

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

      

        setTitle("調光手電筒   2010-3-20");

      

        //建立主發光區TextView的對象;

        final TextView tv_light=(TextView) findViewById(R.id.tv_light);

      

      

        //建立三個Button的對象;

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

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

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

      

        //退出按鈕;

        bt_exit.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                // Perform action on click

             finish();

            }

        });

        //低亮度按鈕;

        bt_low.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                // Perform action on click

            

    tv_light.setBackgroundColor(Color.GRAY);

             

            }

        });

      

        //高亮度按鈕;

        bt_high.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                // Perform action on click

             tv_light.setBackgroundColor(Color.WHITE);

            

            }

        });

      

    }

}

 

    (2)用Activity直接實現OnClickListener介面的方式:

    與上面的程式結構不同,在聲明Class時可以直接實現OnClickListener,然後在Class內部就可以用同一個onClick方法實現所有按鈕的響應,按鈕區分可採用swith--case的語句結構實現。

    例如:public class mmact extends Activity implements OnClickListener, 在class聲明時不僅從Activity派生,而且還實現OnClickListener 介面。在class中用語句實現介面OnClickListener 的onClick方法,即public void onClick(View aView)。對於很多按鈕事件,可以用switch--case實現,例如

     

        switch (aView.getId()) { 

            case R.id.button1: 

                setTitle("Start the App"); 

              

                EditText edt = (EditText) findViewById(R.id.edittext_name); 

                TextView tv = (TextView) findViewById(R.id.textview_display); 

                tv.setText("Hello," + edt.getText()); 

              

              

              

                //android:text="YES" ;

 

                break; 

            case R.id.button2: 

                finish(); 

                break; 

        } 

    下面是個比較完整的程式結構: www.2cto.com

 

public class mmact extends Activity implements OnClickListener { 

    

    @Override

    public void onCreate(Bundle savedInstanceState) { 

         

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.main); 

        //從資源檔中擷取按鈕對象的引用 

        Button sButton1 = (Button)findViewById(R.id.button1); 

        Button sButton2 = (Button)findViewById(R.id.button2); 

        //註冊點擊事件監聽者 

        sButton1.setOnClickListener(this); 

        sButton2.setOnClickListener(this); 

    } 

    

    public void onClick(View aView) { 

         

        switch (aView.getId()) { 

            case R.id.button1: 

                setTitle("Start the App"); 

              

                EditText edt = (EditText) findViewById(R.id.edittext_name); 

                TextView tv = (TextView) findViewById(R.id.textview_display); 

                tv.setText("Hello," + edt.getText()); 

              

              

              

                //android:text="YES" ;

 

                break; 

            case R.id.button2: 

                finish(); 

                break; 

        } 

         

         

    } 

}

  

 

摘自 jcli--部落格

聯繫我們

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