2010-08-17 60 views
0

我一直在试图获得对话框的句柄。我阅读了Android开发站点的信息和一打bolg's。看起来有几种方法可以做到这一点,我至少可以通过3个按钮进行对话(不使用自定义布局的自定义对话框)。Android对话框 - 困惑

如果我用finish(),cancel()等类似的东西来设置正面/负面/中立的动作,它可以处理这些调用。

但是,我想要做的是让按钮做更多的事情,如果只使用主代码中定义的字符串(而不是吐司)显示文本。最后,我想在对话框中输入一些数字并将它们以字符串形式返回。是的,我可以从另一个活动屏幕上做到这一点,但不要因为我喜欢对话框的紧凑尺寸。

即使作弊和返回一个整数到Maincode做一些开关/案件的东西会没事的,但是,我似乎甚至不能够返回一个整数。

据我所知,我需要做一个自定义的警报对话框来做输入的东西,以下是我试图返回一个字符串或整数的尝试 - 它似乎并没有让我在那里!

此代码显示带有3个按钮的对话框。这只是我所做的一个尝试(整数返回的东西删除)...

我能做些什么来从对话框的按钮返回一个整数到Maincode?没有代码错误或警告,它就像我希望不工作...

public class Maincode extends Activity { 
public static String rtndMessage = "Push Button"; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    final TextView text = (TextView) findViewById(
      R.id.TextView01); 
    final Button doIt = (Button) findViewById(R.id.Button01); 

    // ----------------------------------------------------- 
    doIt.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // do something 
      Dialog1();   // call Dialog1 
     } 
    }); // end ----------------------------------------------- 
// do the rest of the code (ie, display the result of doIt) 
text.setText(rtndMessage); // set text w/result 
}//end onCreate ---------------------------------------------- 

public void Dialog1() { 
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setTitle("Dialog Test"); 
builder.setIcon(R.drawable.redtest); 
// chain the following together 
    builder.setMessage("Send text: Yes, No, Cancel") 
    // the positive action ---------------------------------- 
    .setPositiveButton("Yes Action", new 
     DialogInterface.OnClickListener(){ 
     public void onClick(DialogInterface dialog, int id){    
      Maincode.rtndMessage = "sent Yes back";    
     } 
    }) 
    // The negative action ---------------------------------- 
    .setNegativeButton("No Action", new 
      DialogInterface.OnClickListener(){ 
     public void onClick(DialogInterface dialog, int id){ 
      Maincode.rtndMessage = "sent No back"; 
     } 
    }) 
    // The negative action ------------------------------ 
    .setNeutralButton("Cancel", new 
      DialogInterface.OnClickListener(){ 
     public void onClick(DialogInterface dialog, int id){ 
      Maincode.rtndMessage = "sent N/A back"; 
      dialog.cancel(); // just return to activity 
     }   
});  

builder.show(); //显示对话框 } //结束对话框 } //结束活动

+0

您应该编辑您的问题,以便代码正确显示(确保每行前有4个空格)。那么它会更容易帮助你:) – stealthcopter 2010-08-17 21:35:58

回答

4

您可以将对话框实现为单独的活动并获得您需要的任何行为。只需将标准android Theme.Dialog主题应用于该活动,使其看起来像对话框一样。你也可以创建你自己的Theme.dialog作为父项的主题。

+0

谢谢 - 我会尝试Theme.Dialog – headscratch 2010-08-17 21:39:39

+0

嗨,你能举个例子吗? – RaagaSudha 2012-02-10 06:01:30

1

Android 1.6+为您的活动提供了一组易用的对话框功能。

制作对话框时,只需在对话框的按钮的onClick()方法中执行任何操作即可。你可以在DIALOG_CONFIRM_DELETE开关中看到我在下面看到的一个例子,其中我删除了一个本地数据库的记录,并且查询了一个游标。

我想看看http://d.android.com开发指南中的“创建对话框”部分它向您展示了如何使用对话框功能。 http://developer.android.com/guide/topics/ui/dialogs.html

@Override 
protected Dialog onCreateDialog(int id) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 

    switch (id) { 
     case DIALOG_CONFIRM_DELETE: 
      builder 
       .setTitle("Confirm") 
       .setMessage("Are you sure you want to delete that access point?") 
       .setCancelable(true) 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         mDbAdapter.delete(SELECTED_AP_ID); 
         mCursor.requery(); 
        } 
       }).setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         dialog.dismiss(); 
        } 
       }); 
      return builder.create(); 
     case DIALOG_EXPORTING: 
      ProgressDialog progressDialog = new ProgressDialog(this); 
      progressDialog.setMessage("Exporting..."); 
      progressDialog.setCancelable(false); 
      progressDialog.show(); 
      return progressDialog; 
     case DIALOG_GPS_DISABLED: 
      builder 
       .setTitle("GPS signal not Found") 
       .setMessage("GPS is not enabled, and accuracy may be effected.") 
       .setCancelable(false) 
       .setNeutralButton("Ok", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         startService(); 
         dialog.dismiss(); 
        } 
       }); 
      return builder.create(); 
     default: 
      return null; 
    } 
} 
1

这是很迟,但希望这将帮助其他人的未来。 我所做的就是创建实现OnClickListener自定义对话框:

public class LoginDialog extends Dialog implements OnClickListener{ 

    public LoginDialog(Context context) { 
    super(context); 
    this.context = context; 
    // TODO Auto-generated constructor stub 

    } 

    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.dialog); 
    setTitle("Enter Login Details"); 
    setCancelable(true); 

    etUsername = (EditText) findViewById(R.id.etUsername); 
    etPassword = (EditText) findViewById(R.id.etPassword); 
    bLogin = (Button) findViewById(R.id.bDialogLogin); 
    bCancel = (Button) findViewById(R.id.bDialogCancel); 

    bLogin.setOnClickListener(this); 
    bCancel.setOnClickListener(this); 


    } 

    public void onClick(View v) { 

    switch (v.getId()) { 
    case R.id.bDialogLogin: 
     //Check credentials. If they pass: 
     cancel(); 

     //if they don't pass, throw an alert or something 
     break; 

    case R.id.bDialogCancel: 
     dismiss(); 
     break; 
    } 

    } 
} 

然后在我的主代码我实现了一个onCancelListener(对话有onCancel和onDismiss听众都为您准备)。

//This calls my alert dialog. Place this in the onClick of a button or something 
loginDialog = new LoginDialog(this); 
loginDialog.show(); 


loginDialog.setOnCancelListener(new OnCancelListener() { 

     @Override 
     public void onCancel(DialogInterface dialog) { 
      // TODO Auto-generated method stub 

      //This code is run whenever the cancel(); function in the dialog is called. 

     } 
    }); 

希望这有助于!