2017-06-02 16 views
0

我試圖創建按鈕,將執行基於一個簡單的點擊和長時間點擊一些不同的指示,但我很難理解如何把所有東西放在一起。爲每個按鈕執行一個定義的方法是可以的,但我認爲使用onClickListeners會更好一些,不是嗎?不同的onClick和onLongClick方法的兩個按鈕

所以我的代碼如下。正如你所看到的,我試圖捕捉每個按鈕的兩種類型的事件,但是當我按下按鈕1A時,我得到了2A的敬酒,當我點擊按鈕2A時,出現錯誤,應用程序崩潰。 要解決的第二件事是將onClick和onLongClick綁定在一起。

activity_scout.xml

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
android:layout_height="match_parent" 
android:layout_width="match_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
tools:context="com.example.android.scout.ScoutActivity"> 

<LinearLayout 
    android:id="@+id/activity_scout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="8dp" 
    android:paddingLeft="8dp" 
    android:paddingRight="8dp" 
    android:paddingTop="8dp" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/but1A" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="1a" 
     android:onClick="click1a" 
     /> 

    <Button 
     android:id="@+id/but2A" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="2a" 
     android:onClick="click2a" /> 


    </LinearLayout> 
</ScrollView> 

ScoutActivity.java

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 

import static com.example.android.basketscout.R.id.butPlayer1A; 
import static com.example.android.basketscout.R.id.butPlayer2A; 
import static com.example.android.basketscout.R.id.butPlayer3A; 
import static com.example.android.basketscout.R.id.butPlayer4A; 
import static com.example.android.basketscout.R.id.butPlayer5A; 
import static com.example.android.basketscout.R.id.textView; 

public class ScoutActivity extends AppCompatActivity { 

    Button but1A; 
    Button but2A; 
    Button but3A; 
    Button but4A; 
    Button but5A; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_scout); 

     but1A = (Button) findViewById(R.id.but1A); 
     but2A = (Button) findViewById(R.id.but1A); 

     but1A.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view){ 
       Toast.makeText(getApplicationContext(), "Button 1A clicked", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     but2A.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view){ 
       Toast.makeText(getApplicationContext(), "Button 2A clicked", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     but1A.setOnLongClickListener(new View.OnLongClickListener(){ 
      public void onLongClick (View view) { 
       Toast.makeText(getApplicationContext(),"Button 1A long clicked", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

    but2A.setOnLongClickListener(new View.OnLongClickListener(){ 
     public void onLongClick (View view) { 
      Toast.makeText(getApplicationContext(),"Button 2A long clicked", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    } 

} 

[如果你看到這樣未閉合parentheris或不完全正確的變量名任何錯誤,這是因爲從副本的一些編輯/粘貼我做過]

+0

刪除'安卓的onClick = 「click1a」'和'安卓的onClick = 「click2a」'。這是多餘的。 – AlphaQ

+0

錯誤日誌?? ... –

+0

@alphaQ,爲什麼這是多餘的?他們不是兩個不同的按鈕? – GondraKkal

回答

1

您正在查找兩次相同的視圖,必須更改代碼的這一部分:

but1A = (Button) findViewById(R.id.but1A); 
but2A = (Button) findViewById(R.id.but1A); 

要這樣:

but1A = (Button) findViewById(R.id.but1A); 
but2A = (Button) findViewById(R.id.but2A); 

此外,從佈局刪除屬性,它是多餘的,並導致衝突。

<Button 
     android:id="@+id/but1A" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="1a"/> 

    <Button 
     android:id="@+id/but2A" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="2a"/> 
+0

好的,一個問題解決了,但第二個問題呢?我如何添加onLongClick方法? – GondraKkal

+0

你正在用編程方式使用OnLongItemClickListener –

0

在XML文件中刪除您的onClick輸入,如果你宣佈從XML一個onClick方法你不需要調用上的按鈕setOnClickListener對象,而不是隻是創建方法與yourOnClickMethod(View view)

0

參數你的代碼有錯誤。首先是已經指出@Luiz這是着眼於按鈕對象的綁定:

but2A = (Button) findViewById(R.id.but2A); 

而且,這裏有一個線程可以幫助你,因爲我知道你已經宣佈onClick屬性,在XML的Button標籤:How exactly does the android:onClick XML attribute differ from setOnClickListener?

基本上有兩種實現點擊監聽器的方法,如果您使用的是setOnClickListener()那麼onClick屬性不需要XML,反之亦然。

0

問題是你找到相同的ID兩次:

but1A = (Button) findViewById(R.id.but1A); 
    but2A = (Button) findViewById(R.id.but1A); 

but1A = (Button) findViewById(R.id.but1A); 
    but2A = (Button) findViewById(R.id.but2A); 

然後,

對於長點擊監聽的觀點看這個how to implement a long click listener on a listview

0

取出的onClick屬性來自 按鈕佈局

,正確的驗證碼

but1A = (Button)findViewById(R.id.but1A); 
but2A = (Button)findViewById(R.id.but1A); 

but1A = (Button)findViewById(R.id.but1A); 
but2A = (Button)findViewById(R.id.but2A); 
相關問題