2014-05-15 65 views
-1

我想创建2或android系统中超过2两按钮,但我在这行如何创建2个或2个以上按钮的Android

View btnClick = findViewById(R.id.buttonClick); 
    View btnscan = findViewById(R.id.btscanClick); 
    //set event listener 
    btnClick.setOnClickListener(this); 
} 

//override the OnClickListener interface method 
@Override 
public void onClick(View arg0) { 
    if(arg0.getId() == R.id.buttonClick){ 
     //define a new Intent for the second Activity 
     Intent intent = new Intent(this,SecondActivity.class); 
     //start the second Activity 
     this.startActivity(intent); 

public void onClick1(View arg1) { 
    if(arg1.getId() == R.id.btscanClick){ 
     //define a new Intent for the second Activity 
     Intent intent = new Intent(this,ScanActivity.class); 
     //start the second Activity 
     this.startActivity(intent); 
+2

你得到了哪个问题,并在哪一行?发布logcat。 –

+0

发布您的清单文件。 –

+0

发布您的logcat文件.... – User11

回答

0

可能是问题将与你的设计,我的事情你是在表面观实现它。如果你这样做让与的LinearLayout和RelativeLayout的布局,这将是更好,如果你还可以添加你的.manifestfile

0

嘿投你的看法按钮即代替得了的问题的

View btnscan = findViewById(R.id.btscanClick);

使用

Button btnscan = (Button)findViewById(R.id.btscanClick);

也会发布您收到的错误,以便我可以帮助您。

+0

不一定需要投射,因为您可以看到OP将其分配给'View' not'Button'。 –

0

添加监听到其他按钮以及

Button btnscan = (Button)findViewById(R.id.btscanClick); 
btnscan.setOnClickListener(this); 

Button btnclick = (Button)findViewById(R.id.buttonClick); 
btnclick.setOnClickListener(this); 

而让处理只有一个OnClick方法点击。

public void onClick(View arg0) { 

if(arg0.getId() == R.id.buttonClick){ 
    //define a new Intent for the second Activity 
    Intent intent = new Intent(this,SecondActivity.class); 
    //start the second Activity 
    this.startActivity(intent); 
    } 
else if(arg1.getId() == R.id.btscanClick){ 
    //define a new Intent for the second Activity 
    Intent intent = new Intent(this,ScanActivity.class); 
    //start the second Activity 
    this.startActivity(intent); 
    } 
} 
0

你平时想去定义OnClickListener的按钮的方法是如下:

btnClick.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View arg0) { 
      //define a new Intent for the second Activity 
      Intent intent = new Intent(this,SecondActivity.class); 
      //start the second Activity 
      this.startActivity(intent); 
    } 
}); 

btnScan.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View arg0) { 
      //define a new Intent for the scan Activity 
      Intent intent = new Intent(this,ScanActivity.class); 
      //start the second Activity 
      this.startActivity(intent); 
    } 
}); 

这意味着您为每个按钮使用不同的onClickListener。

+0

我解决了这个问题,但按钮onclick没有运行。 – user3640392

相关问题