2014-05-15 114 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

相關問題