2014-02-20 2 views
2

내 응용 프로그램에 대한 사용자 정의 토스트를 만들고 있습니다. ToClick에 추가 한 버튼에 OnClickListener를 추가하면됩니다. 모든 것이 잘되고 버튼을 볼 수 있지만 OnClick에는 응답하지 않습니다. 어떤 생각.사용자 토스트에 클릭 리스너를 추가 할 수 있습니까

예제 코드 : 또한

Button button = new Button(getApplicationContext()); 
      button.setText("Click Me"); 
      button.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        ProgressDialog.show(getApplicationContext(), "Hello", "nothing"); 

       } 
      }); 
     button.setLayoutParams(new  ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)); 
     Toast toast = new Toast(getApplicationContext()); 
     toast.setGravity(Gravity.BOTTOM, 0, 0); 
     toast.setMargin(0,-80); 
     toast.setDuration(Toast.LENGTH_LONG); 
     toast.setView(button); 
     toast.show(); 

, 나는 다음과 같은 버튼에 onTouchListener를 추가하여 노력했다.

button.setOnTouchListener(new OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     ProgressDialog.show(getApplicationContext(), "Hello", "nothing"); 
     return false; 
    } 
}); 

하지만 작동하지 않습니다.

+1

대신 토스트의 대화 – Raghunandan

+0

뷰는 클릭 수 토스트 지금에 AlertDialog 또는 popupwindow를 사용하여 내부되지 않습니다 사용할 수 있습니다. –

+0

건배에 단추를 사용하는 것은 나쁜 생각입니다. 대화 상자를 사용하십시오. – JesusS

답변

1

Toast에는 Button을 포함하면 안됩니다. 버튼을 표시 한 다음 일정 기간 후에 버튼을 숨 깁니다. 기존 레이아웃 위에 RelativeLayout을 추가하여이 작업을 수행 할 수 있습니다. 이런 식으로 뭔가 작업을해야합니다 :Toast처럼 버튼 onCreate에 다음

public void showDialog(View v) { 
    if (v.getId() == R.id.toast_button) { 
     ProgressDialog.show(this, "Hello", "nothing"); 
    } 
} 

보여 :

이제
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <include layout="@layout/main" /><!-- References your existing layout file --> 
    <Button 
     android:id="@+id/toast_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_alignParentBottom="true" 
     android:visibility="gone" 
     android:text="@string/click_me" 
     android:onClick="showDialog" /><!-- Should reference a String resource "click me"--> 
</RelativeLayout> 

에서, 토스트 효과를 당신의 Activity에 다음 메서드를 추가

final Button b = (Button) findViewById(R.id.toast_button); 
//optionally add some animations for fading in 
b.setVisibility(View.VISIBLE); 
Timer t = new Timer(); 
t.schedule(new TimerTask() { 
    @Override 
    public void run() { 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       //optionally add some animations for fading out 
       b.setVisibility(View.GONE); 
      } 
     }); 
    } 
}, 1000); 
+0

이것은 Phil이 아닌 경우입니다. 버튼이 클릭 이벤트를 수신하지 않습니다. – Adnan

+0

@Adnan 수정 된 답변보기 – Phil

관련 문제