2014-05-15 49 views
-1

我不知道如何將對話框功能添加到我的onclick功能,我希望它對話框被召喚一旦我點擊按鈕。如何在onclick函數中調用我的對話框功能?

這是onCreate方法

/** Called when the activity is first created. */ 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.listview); 
    fillData(); 
    boxAdapter = new ListAdapter(this, products); 

    ListView lvMain = (ListView) findViewById(R.id.lvMain); 
    lvMain.setAdapter(boxAdapter); 


    Button btn = (Button) findViewById(R.id.insert); 

    OnClickListener listener = new OnClickListener() {   
     @Override 
     public void onClick(View v) {        


     } 
    }; 

    /** Setting the event listener for the add button */ 
    btn.setOnClickListener(listener);  



    } 

那麼這是我的對話功能,這是在同一個Java類

public Dialog onCreateDialog(Bundle savedInstanceState) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     // Get the layout inflater 
     LayoutInflater inflater = this.getLayoutInflater(); 

     // Inflate and set the layout for the dialog 
     // Pass null as the parent view because its going in the dialog layout 
     builder.setView(inflater.inflate(R.layout.dialog, null)) 
     // Add action buttons 
      .setPositiveButton(R.string.Insert, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 

       } 
      }) 
      .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // DialogFragment.this.getDialog().cancel(); 
       } 
      });  
     return builder.create(); 
    } 
+0

獲取答案在這裏http://stackoverflow.com/問題/ 18842277 /對話框泛型函數,返回布爾基於對用戶的按鈕按下/ 18842412#18842412 –

回答

0

這裏的答案,讓包作爲最終

public void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.listview); 
    fillData(); 
    boxAdapter = new ListAdapter(this, products); 

ListView lvMain = (ListView) findViewById(R.id.lvMain); 
lvMain.setAdapter(boxAdapter); 


Button btn = (Button) findViewById(R.id.insert); 

OnClickListener listener = new OnClickListener() {   
    @Override 
    public void onClick(View v) {        
      Dialog d = onCreateDialog(savedInstanceState); 
      d.show(); 

    } 
}; 

/** Setting the event listener for the add button */ 
btn.setOnClickListener(listener);  

}

0

在你的對話框功能,您不必傳遞參數Bundle savedInstanceState,然後在onClick功能爲您的按鈕這樣調用該函數:

OnClickListener listener = new OnClickListener() {   
     @Override 
     public void onClick(View v) {        
Dialog dialog = onCreateDialog(); // assuming that you will remove the argmunent 
dialog.show(); 

     } 
    }; 
相關問題