2016-09-07 14 views
0

現在、レイアウトを試していて新しいページを開いていますが、OnLongClickListenerを使って1つのページを開きますが、同じテキストビューボックスを使ってOnClickListenerを使ってトーストしていきたいと思います。OnLongClickを使って別のXMLレイアウトを開くことができます

これはこれまで私が設計したものです。長いクリックでブール値を出力する必要があるため、これは機能しません。
アイデア?

XMLメイン

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="com.example.android.how_to_use.MainActivity" 
    android:id="@+id/main_view"> 

<TextView 
    android:id="@+id/long_press_textView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Long Press To Open \n'Drag n Drop'"/> 

</LinearLayout> 

XML 2ページ目

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"> 

<ImageView 
    android:id="@+id/swirl_img" 
    android:src="@drawable/Swirl" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

</LinearLayout> 

MainActivity.Java

public class MainActivity extends AppCompatActivity { 

    private LinearLayout mainView; 
    private TextView txtView; 

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

    txtView = (TextView) findViewById(R.id.long_press_textView); 
    addListenerLongPressBtn(); 
    addListenerOnClickBtn(); 
} 

public void addListenerOnClickBtn() { 
    txtView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Toast.makeText(MainActivity.this, "Press Secret Button for Longer", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

    public void addListenerLongPressBtn() { 

     txtView.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View view) { 
       Intent intent = new Intent(this, DragDropActivity.class); 
       startActivity(intent); 
       return true; 
      } 
     }); 
    } 

} 
+0

オリジナルはもっとありますが、私は非相対コードを無視しています。 – tom1988

答えて

0

私は私自身の問題への解決策を見つけました。

public void addListenerLongPressBtn() { 

     txtView.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View view) { 
       Intent intent = new Intent(view.getContext(), DragDropActivity.class); 
       startActivity(intent); 
       return true; 
      } 
     }); 
    } 
関連する問題