android MainActivity继承onclicklistener 重写的onclick方法怎么用
publicclassMainActivityextendsActivityimplementsOnClickListener{privateButtonbutton1,...
public class MainActivity extends Activity implements OnClickListener {
private Button button1, button2, button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onClick(DialogInterface dialog, int which) {
button1 = (Button) this.findViewById(R.id.button1);
button2 = (Button) this.findViewById(R.id.button2);
button3 = (Button) this.findViewById(R.id.button3);
switch (key) {//这里应该填写什么呢?是不是which?
case value://value应该填写什么呢?我用button.getID();可是eclipse报错。
break;
default:
break;
}
}
}
//重写的onclick方法该如何用switc来区分每一个不同的button呢?
求大神赐教! 展开
private Button button1, button2, button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onClick(DialogInterface dialog, int which) {
button1 = (Button) this.findViewById(R.id.button1);
button2 = (Button) this.findViewById(R.id.button2);
button3 = (Button) this.findViewById(R.id.button3);
switch (key) {//这里应该填写什么呢?是不是which?
case value://value应该填写什么呢?我用button.getID();可是eclipse报错。
break;
default:
break;
}
}
}
//重写的onclick方法该如何用switc来区分每一个不同的button呢?
求大神赐教! 展开
3个回答
展开全部
首先纠正一下,不是继承OnClickListener,而是实现,要严格区分继承和实现,他们是不一样的.其次你说的那个问题.这里的key为button的id,即你在布局中标记的id,通过重写onClick(View v)方法中View参数v.getId()获取,然后依次用布局中的id来和v.getId()做比较,如:button1的id为R.id.button1,那么如果当v.getId()和button1的id相等时则为button1做相应的事件处理,button2,button3也一样,最终代码为:
public class MainActivity extends Activity implements OnClickListener {
private Button button1, button2, button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) this.findViewById(R.id.button1);
button2 = (Button) this.findViewById(R.id.button2);
button3 = (Button) this.findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {//这里应该填写什么呢?是不是which?
case R.id.button1://value应该填写什么呢?我用button.getID();可是eclipse报错。
//doSomething
break;
case R.id.button2:
//doSomething
case R.id.button3:
//doSomething
default:
break;
}
}
}
public class MainActivity extends Activity implements OnClickListener {
private Button button1, button2, button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) this.findViewById(R.id.button1);
button2 = (Button) this.findViewById(R.id.button2);
button3 = (Button) this.findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {//这里应该填写什么呢?是不是which?
case R.id.button1://value应该填写什么呢?我用button.getID();可是eclipse报错。
//doSomething
break;
case R.id.button2:
//doSomething
case R.id.button3:
//doSomething
default:
break;
}
}
}
展开全部
public class MainActivity extends Activity implements OnClickListener {
private Button bt1,bt2,bt3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1=(Button) findViewById(R.id.button1);
bt2=(Button) findViewById(R.id.button2);
bt3=(Button) findViewById(R.id.button3);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
Intent intent=new Intent(this,AA.class);
startActivity(intent);
break;
case R.id.button2:
Intent intent2=new Intent(this,BB.class);
startActivity(intent2);
break;
case R.id.button3:
Intent intent3=new Intent(this,CC.class);
startActivity(intent3);
break;
}
}
}
private Button bt1,bt2,bt3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1=(Button) findViewById(R.id.button1);
bt2=(Button) findViewById(R.id.button2);
bt3=(Button) findViewById(R.id.button3);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
Intent intent=new Intent(this,AA.class);
startActivity(intent);
break;
case R.id.button2:
Intent intent2=new Intent(this,BB.class);
startActivity(intent2);
break;
case R.id.button3:
Intent intent3=new Intent(this,CC.class);
startActivity(intent3);
break;
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你是不是实现的接口写错了?怎么会覆盖那个方法。
应该implements View.OnClickListener这个接口,会提示你覆盖这个public void onClick(View v) 方法。然后在switch id 就对了
应该implements View.OnClickListener这个接口,会提示你覆盖这个public void onClick(View v) 方法。然后在switch id 就对了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询