欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Android中點(diǎn)擊事件的幾種實(shí)現(xiàn)方式

 更新時(shí)間:2016年12月19日 16:05:23   作者:潘侯爺  
本篇文章主要介紹了Android中點(diǎn)擊事件的實(shí)現(xiàn)方式,點(diǎn)擊事件的實(shí)現(xiàn)分為3中,詳細(xì)的介紹了三種的用法,有興趣的可以了解一下。

在之前博文中多次使用了點(diǎn)擊事件的處理實(shí)現(xiàn),有朋友就問了,發(fā)現(xiàn)了很多按鈕的點(diǎn)擊實(shí)現(xiàn),但有很多博文中使用的實(shí)現(xiàn)方式有都不一樣,到底是怎么回事。今天我們就匯總一下點(diǎn)擊事件的實(shí)現(xiàn)方式。

點(diǎn)擊事件的實(shí)現(xiàn)大致分為以下三種:

(1)Activity 實(shí)現(xiàn)接口方式實(shí)現(xiàn)點(diǎn)擊事件(經(jīng)常使用)

(2)自定義方法,使用配置文件android:onclick

(3)使用內(nèi)部類方式實(shí)現(xiàn)

(4)使用匿名內(nèi)部類實(shí)現(xiàn)介紹下幾種點(diǎn)擊事件的實(shí)現(xiàn)方式:

下面我們通過代碼來簡(jiǎn)單演示下幾種點(diǎn)擊事件的實(shí)現(xiàn)方式:

(1)Activity 實(shí)現(xiàn)接口方式實(shí)現(xiàn)點(diǎn)擊事件

/**
 * Activity 實(shí)現(xiàn)接口方式實(shí)現(xiàn)點(diǎn)擊事件
 * Activity 實(shí)現(xiàn) View.OnClickListener 實(shí)現(xiàn) onClick(View view){} 方法
 * 在 Activity 的 onCreate 方法中注冊(cè)事件
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  private Button btn;
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.button);
    btn.setOnClickListener(this);
  }
  public void onClick(View v) {
    Toast.makeText(MainActivity.this,"實(shí)現(xiàn)接口方式",Toast.LENGTH_LONG).show();
  }
}

(2)自定義方法,使用layout配置文件android:onclick

/**
 * 使用配置文件方式實(shí)現(xiàn)點(diǎn)擊事件
 * 在layout中的配置文件中使用onClick 屬性指定觸發(fā)事件時(shí)的處理方法,
 * 在 Activity 中提供一個(gè)同名的方法 格式為 public void XXX(View v){....}
 */
public class MainActivity extends AppCompatActivity {
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }
  public void click(View v){
    Toast.makeText(MainActivity.this,"自定義",Toast.LENGTH_LONG).show();
  }
}

(3)使用內(nèi)部類方式實(shí)現(xiàn)

/**
 * 使用內(nèi)部類方式實(shí)現(xiàn)點(diǎn)擊事件
 * 定義一個(gè) View.OnClickListener 的實(shí)現(xiàn)類,實(shí)現(xiàn) onClick 方法。
 * 在 Activity 的 onCreate 方法中注冊(cè)事件
 */
public class MainActivity extends AppCompatActivity {
  private Button btn;
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.button2);
    btn.setOnClickListener(new Listener());
  }
  class Listener implements View.OnClickListener{
    @Override
    public void onClick(View v) {
      Toast.makeText(MainActivity.this,"內(nèi)部類",Toast.LENGTH_LONG).show();
    }
  }
}

(4)使用匿名內(nèi)部類實(shí)現(xiàn)

/**
 * 使用匿名內(nèi)部類方式實(shí)現(xiàn)點(diǎn)擊事件
 * 注冊(cè)按鈕單擊事件時(shí)直接使用匿名內(nèi)部類
 */
public class MainActivity extends AppCompatActivity {
  private Button btn;
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.button3);
    btn.setOnClickListener(new View.OnClickListener(){
      @Override
      public void onClick(View v) {
        Toast.makeText(MainActivity.this,"匿名內(nèi)部類",Toast.LENGTH_LONG).show();
      }
    });
  }
}

補(bǔ)充:

不同的組件來實(shí)現(xiàn)不同類型的點(diǎn)擊事件,例如onItemClickListener,OnCheckedChangeListener,OnRatingBarChangeListener,OnMultiChoiceClickListener,OnDate/timeSetListener,OnScrollListener,OnChildClickListener,setOnTouchListener,OnPageChangeListener,OnMenuItemClickListener,OnEditorActionListener,OnEditorActionListener等等各種點(diǎn)擊事件的處理形式。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論