2013-07-31 2 views
1

앱이 시작되지만 Select_Players 버튼을 클릭하면 대화 상자가 내 기기에 표시되지 않습니다. 다음은 코드입니다 : 나는 대화 방법은 대화가 반환 볼Android setOnClick이 작동하지 않음/Dialogbox가 표시되지 않음

public class MainActivity extends Activity { 


    private Button selectPlayers; 

      @Override 
     protected void onCreate(final Bundle savedInstanceState) { 

      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_demo); 

      super.onStart(); //customize 
      super.onResume(); //customize 

      selectPlayers = (Button) findViewById(R.id.add_players); 

      selectPlayers.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View view) { 

        // Launch dialogbox on click 
        onCreateDialog(savedInstanceState); 

       } 
      }); 
     } 

     public Dialog onCreateDialog(Bundle savedInstanceState) { 

      @SuppressWarnings("rawtypes") 
      final ArrayList mSelectedItems = new ArrayList(); // Where we track the selected items 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      // Set the dialog title 
      builder.setTitle(R.string.select_players) 

      // Specify the list array, the items to be selected by default (null for none), 
      // and the listener through which to receive callbacks when items are selected 
      .setMultiChoiceItems(R.array.players_name, null, 
        new DialogInterface.OnMultiChoiceClickListener() { 

       @SuppressWarnings("unchecked") 
       @Override 
       public void onClick(DialogInterface dialog, int which, 
         boolean isChecked) { 
        if (isChecked) { 
         // If the user checked the item, add it to the selected items 
         mSelectedItems.add(which); 
        } else if (mSelectedItems.contains(which)) { 
         // Else, if the item is already in the array, remove it 
         mSelectedItems.remove(Integer.valueOf(which)); 
        } 
       } 
      }) 

      // Set the action buttons 
      .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int id) { 

        //CODE TO CLOSE DIALOGBOX AND START FORGE 

       } 
      }) 
      .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int id) { 

        //CODE TO JUST CLOSE DIALOGBOX 

       } 
      }); 

      return builder.create(); 
     } 
} 

그러나 나는 그것이 온 클릭의 결과로 나타나게하는 방법을 잘 모르겠어요? (참고로 Android 개발자 웹 사이트에서 Dialog 메서드를 사용했습니다.)

고마워요! 당신은 당신의 onclick이 같은 일을 구축 할 수 있도록

답변

1

이 방법은 대화를 반환

 Dialog d = onCreateDialog(savedInstanceState); 
    d.show(); 

나는 무엇을 시도했다하면 활동 방법이 onCreateDialog()를 오버라이드 (override)하는 것입니다 생각하지만, 당신은 그것을 할 필요가 antoher 방법으로, 등이 여기에 표시 :

http://www.mysamplecode.com/2011/11/android-alertdialog-example-showdialog.html

+0

그냥 대화 상자를 표시하는 데 효과적입니다. 감사합니다. – newenthusiast

0

을 시도해보십시오

selectPlayers.setOnClickListener(new Button.OnClickListener() { 

      @Override 
      public void onClick(View view) { 

       // Launch dialogbox on click 
       onCreateDialog(savedInstanceState); 

      } 
     }); 

항상 View Extending 오브젝트 내부의 Listener를 사용하십시오. 코드에서 "보기"를 "버튼"으로 변경했습니다. 아직 테스트하지 않았으므로 많은 것을 좋아한다는 것을 기억합니다. 하지만 마지막 해의 문제는 garbadge가 수집되는 Week Hashmaps에서 안드로이드 API 인라인 청취자 방식에 따라 달라집니다. 이것이 문제가되는 경우 솔루션은 매우 간단합니다. 리스너를 변수에 연결하고 해당 변수를 전달하십시오.

예 :

Button.OnClickListener listener = new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 

       // Launch dialogbox on click 
       onCreateDialog(savedInstanceState); 

      } 
     }; 
selectPlayers.setOnClickListener(listener) 

활동이 청취자와 버튼에 해당 인스턴스를 전달을 보유하고 이쪽으로. 이제 가비지 수집기가 작업을 제거하거나 수동으로 제거 할 때까지 가비지 수집기가 제거하지 않아야합니다.

아 .. "onCreateDialog"에 대한 호출을 보지 못했습니다. "on"으로 시작하는 첫 번째 메소드는 라이프 사이클 메소드이며 시스템에서 수동으로 호출하지는 않습니다. 그렇다면 onCreateDialog는 더 이상 사용되지 않습니다. 이 문제에 대해서는 Opiatefuchs의 대답을 참조하십시오.

0

다음과 같이 사용해야하는 show() 함수가 없습니다.

dialog.show();

0

먼저, onCreate()의 버튼에 리스너를 설정합니다.

private Button selectPlayers; 
    @Override 
protected void onCreate(final Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_demo); 

    super.onStart(); //customize 
    super.onResume(); //customize 

    selectPlayers = (Button) findViewById(R.id.add_players); 
    //set the listener to the button 
    selectPlayers.setOnClickListener(this) 
} 

대화 상자 코드. 하나의 onclick() 메소드를 갖는

public Dialog onCreateDialog(Bundle savedInstanceState) { 
    @SuppressWarnings("rawtypes") 
    final ArrayList mSelectedItems = new ArrayList(); // Where we track the selected items 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    // Set the dialog title 
    builder.setTitle(R.string.select_players) 

    // Specify the list array, the items to be selected by default (null for none), 
    // and the listener through which to receive callbacks when items are selected 
    .setMultiChoiceItems(R.array.players_name, null, 
      new DialogInterface.OnMultiChoiceClickListener() { 

     @SuppressWarnings("unchecked") 
     @Override 
     public void onClick(DialogInterface dialog, int which, 
       boolean isChecked) { 
      if (isChecked) { 
       // If the user checked the item, add it to the selected items 
       mSelectedItems.add(which); 
      } else if (mSelectedItems.contains(which)) { 
       // Else, if the item is already in the array, remove it 
       mSelectedItems.remove(Integer.valueOf(which)); 
      } 
     } 
    }) 

    // Set the action buttons 
    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int id) { 

      //CODE TO CLOSE DIALOGBOX AND START FORGE 

     } 
    }) 
    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int id) { 

      //CODE TO JUST CLOSE DIALOGBOX 

     } 
    }); 
    AlertDialog alert = builder.create(); 
    alert.show(); 

} 

당신이 arg0에서 다음 대화 상자를 호출하는 데 사용되는 버튼의 경우 여기에, 각각을 구분하는 경우/케이스 문을 사용하여, 하나 개의 방법 등 많은 버튼을 처리 할 수 ​​있음을 의미 대화 상자를 처리하는 메서드를 호출해야합니다.

public void onClick(View arg0) 
{ 
     if(arg0== selectPlayers){ 
     //Show your dialog 
} 
} 

이렇게하면 개인적인 취향입니다. 모든 것이 깔끔하게 정리되어 있지만 내 생각에는 다시 생각납니다.

관련 문제