gpt4 book ai didi

Android - setOnClickListener vs OnClickListener vs View.OnClickListener

转载 作者:IT老高 更新时间:2023-10-28 21:47:43 27 4
gpt4 key购买 nike

我的理解是,当我创建一个监听点击的按钮对象时,我必须:

  1. 创建按钮对象
  2. 使用OnClickListner让它监听用户的点击
  3. 使用onClick在用户点击按钮后执行 Action

现在,

  • setOnClickListener 在哪里符合上述逻辑?
  • 真正听到按钮点击的是哪个?
  • setOnClickListener?
  • OnClickListener?
  • View.OnClickListener?
  • 这三者有什么区别?

这是我在 Android Dev 中找到的:

//The example below shows how to register an on-click listener for a Button.

// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};

protected void onCreate(Bundle savedValues) {
...
// Capture our button from layout
Button button = (Button)findViewById(R.id.corky);
// Register the onClick listener with the implementation above
button.setOnClickListener(mCorkyListener);
...
}

您可能还会发现将 OnClickListener 实现为 Activity 的一部分更为方便。这将避免额外的类负载和对象分配。例如:

public class ExampleActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedValues) {
...
Button button = (Button)findViewById(R.id.corky);
button.setOnClickListener(this);
}

// Implement the OnClickListener callback
public void onClick(View v) {
// do something when the button is clicked
}
}

最佳答案

假设我们有 3 个按钮

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);


// Capture our button from layout
Button button = (Button)findViewById(R.id.corky);
Button button2 = (Button)findViewById(R.id.corky2);
Button button3 = (Button)findViewById(R.id.corky3);
// Register the onClick listener with the implementation above
button.setOnClickListener(mCorkyListener);
button2.setOnClickListener(mCorkyListener);
button3.setOnClickListener(mCorkyListener);

}

// Create an anonymous implementation of OnClickListener
private View.OnClickListener mCorkyListener = new View.OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
// Yes we will handle click here but which button clicked??? We don't know

}
};

}

那我们该怎么办?

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);


// Capture our button from layout
Button button = (Button)findViewById(R.id.corky);
Button button2 = (Button)findViewById(R.id.corky2);
Button button3 = (Button)findViewById(R.id.corky3);
// Register the onClick listener with the implementation above
button.setOnClickListener(mCorkyListener);
button2.setOnClickListener(mCorkyListener);
button3.setOnClickListener(mCorkyListener);

}

// Create an anonymous implementation of OnClickListener
private View.OnClickListener mCorkyListener = new View.OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
// Yes we will handle click here but which button clicked??? We don't know

// So we will make
switch (v.getId() /*to get clicked view id**/) {
case R.id.corky:

// do something when the corky is clicked

break;
case R.id.corky2:

// do something when the corky2 is clicked

break;
case R.id.corky3:

// do something when the corky3 is clicked

break;
default:
break;
}
}
};

}

或者我们可以这样做:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);


// Capture our button from layout
Button button = (Button)findViewById(R.id.corky);
Button button2 = (Button)findViewById(R.id.corky2);
Button button3 = (Button)findViewById(R.id.corky3);
// Register the onClick listener with the implementation above
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something when the corky is clicked
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something when the corky2 is clicked
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something when the corky3 is clicked
}
});

}

}

或者我们可以实现 View.OnClickListener,我认为这是最好的方法:

public class MainActivity extends ActionBarActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);


// Capture our button from layout
Button button = (Button)findViewById(R.id.corky);
Button button2 = (Button)findViewById(R.id.corky2);
Button button3 = (Button)findViewById(R.id.corky3);
// Register the onClick listener with the implementation above
button.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);

}

@Override
public void onClick(View v) {
// do something when the button is clicked
// Yes we will handle click here but which button clicked??? We don't know

// So we will make
switch (v.getId() /*to get clicked view id**/) {
case R.id.corky:

// do something when the corky is clicked

break;
case R.id.corky2:

// do something when the corky2 is clicked

break;
case R.id.corky3:

// do something when the corky3 is clicked

break;
default:
break;
}
}
}

最后这里没有真正的区别只是“比其他更好”

关于Android - setOnClickListener vs OnClickListener vs View.OnClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29479647/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com