2013-08-03 53 views
0

所以我有一個代碼,檢查一些東西,我把它放在活動的onCreate()。我想知道把它放在那裏是否正確,並且由於某些原因,檢查主要活動的代碼根本不起作用,而第二個有一個敬酒的代碼則起作用。我認爲這個問題可能在AlertDialog中。這裏有一個烤麪包:活動代碼的放置位置?

AlertDialog.Builder Dial = new AlertDialog.Builder(Screen.this); 
Dial.setTitle(R.string.Dial_Tit); 
Dial.setMessage(R.string.Dial_Mes); 
Dial.setPositiveButton("OK", PosBC()); 
Dial.setNegativeButton(R.string.Dial_NegBC, NegBC()); 
Dial.show(); 

注意:這兩個按鈕都有方法,我只是沒有發佈它們。問題是警報甚至沒有顯示。而且由於某種原因,吐司確實有效,就像自動點擊按鈕一樣,即使認爲該方法的意圖不起作用。根據要求

更多代碼:

private DialogInterface.OnClickListener NegBC() { 
    Intent moveToStart; 
    moveToStart = new Intent(Screen.this, Launch.class); 
    startActivity(moveToStart); 
    return null; 
} 

private DialogInterface.OnClickListener PosBC() { 
    startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS)); 
    Toast.makeText(getApplicationContext(), R.string.settingsToast, Toast.LENGTH_LONG).show(); 
    return null; 
} 

更新:我已經添加了create()方法,它顯示的對話框中,但它是這樣的:在創建活動時表示敬酒,按回到去設置,從設置顯示對話框中按回,按鈕不起作用。

回答

0

在OnCreateDialog中寫入您的AlertDialog代碼並在OnCreate AsynTask中啓動一個用於檢查目的,一旦您的任務完成,在onPostExecute內關閉對話框。

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

    showDialog(0x01); 

} 

@Override 
protected Dialog onCreateDialog(int id) { 
    AlertDialog.Builder Dial; 
    switch (id) { 

     case 0x01: 
      Dial = new AlertDialog.Builder(Screen.this); 
      Dial.setTitle(R.string.Dial_Tit); 
      Dial.setMessage(R.string.Dial_Mes); 
      Dial.setPositiveButton("OK", PosBC()); 
      Dial.setNegativeButton(R.string.Dial_NegBC, NegBC()); 
      Dial.create(); 
     break; 

    default: 
     break; 
    } 

    return super.onCreateDialog(id); 
} 
+0

你能否詳細說明一下,我是一個noob,並不是很懂。 – user2563892

+0

是不是有一種更簡單的方式來顯示對話框?我的意思是我想要一些我會理解的東西。 – user2563892

+0

應用程序不應該阻塞UI線程,爲什麼檢查操作應該進入不同的線程。 – strike

0

只要你啓動應用程序,並且它沒有被緩存在設備的RAM中,就會調用onCreate()。

我不明白你想要達到的目標以外,請編輯你的文章,增加更多的代碼,我也會編輯我的答案,以便更詳細。

0

使用此代碼在android系統上點擊一個按鈕顯示alertDialog:

 package .....; // name of your package. 

     import android.app.Activity; 
     import android.app.AlertDialog; 
     import android.content.DialogInterface; 
     import android.os.Bundle; 
     import android.view.View; 
     import android.widget.Button; 
     import android.widget.Toast; 


public class AlertDialogActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     Button btnAlertTwoBtns = (Button) findViewById(R.id.button1); 

     btnAlertTwoBtns.setOnClickListener(new View.OnClickListener() { 


      public void onClick(View arg0) { 
       // Creating alert Dialog with two Buttons 

       AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this); 

       // Setting Dialog Title 
       alertDialog.setTitle(" "); //type your title here insid the quotes. 

       // Setting Dialog Message 
       alertDialog.setMessage(" "); //type the message which is to be displayed 

       // Setting Icon to Dialog 
       alertDialog.setIcon(R.drawable.ic_launcher); // set the icon from drawable folder just put the icon file in drawable folder. 

       // Setting Positive "Yes" Button 
       alertDialog.setPositiveButton("YES", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog,int which) { 
           // Write your code here to execute after dialog 
           Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show(); // just a sample code to tell that what things you can do here 
          } 
         }); 
       // Setting Negative "NO" Button 
       alertDialog.setNegativeButton("NO", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           // Write your code here to execute after dialog 
           Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show(); 
           dialog.cancel(); 
          } 
         }); 

       // Showing Alert Message 
       alertDialog.show(); 

      } 
     }); 
} 
} 
+0

單擊按鈕時我不需要它,它必須在if語句中執行。 – user2563892

+0

然後刪除onclick函數和按鈕的東西,並添加如if(something){然後鍵入警報對話框代碼} – Prakhar

0

好吧,我解決我自己,原來這只是邏輯缺失:d。抱歉!

相關問題