单击横幅时如何将列表视图放在警报对话框中?

时间:2013-12-14 06:24:44

标签: android android-listview alertdialog

我是新手,当我点击横幅广告时,我需要AlertDialog ListView,其中有两个选择西班牙语和英语出现。当我点击一个,我需要去一个网站,并选择退回或取消对话。

如果有这个帮助,我所有的(不是ListView

public class MainActivity extends Activity {


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

    final AdView adView = (AdView) findViewById(R.id.adView);
    adView.loadAd(new AdRequest());

     // Internet Connection detector
    ConnectionDetector cd;

    // Alert Dialog Manager
    AlertDialogManager alert = new AlertDialogManager();

    final ImageView Banner = (ImageView) findViewById(R.id.imageView2);
    Banner.setOnClickListener(new OnClickListener(){




        @Override
        public void onClick(View arg0) {

            // custom dialog
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.custom);
            dialog.setTitle("Title...");

            // set the custom dialog components - text, image and button
            TextView text = (TextView) dialog.findViewById(R.id.text);
            text.setText("Android custom dialog example!");
            ImageView image = (ImageView) dialog.findViewById(R.id.image);
            image.setImageResource(R.drawable.ic_launcher);

            Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
            // if button is clicked, close the custom dialog
            dialogButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            dialog.show();
          }
        });
    }
}



            //al apretar click en el banner abre el browser con la pág: las.leagueoflegends.com

            //Intent browserIntent = 
            //  new Intent(Intent.ACTION_VIEW, 
                //  Uri.parse("http://www.facebook.com"));
        //   startActivity(browserIntent);
        }});

PD:我尝试了不同的代码,但我得到了不同的错误D:

3 个答案:

答案 0 :(得分:3)

试试这个..

Banner.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {                   

                String[] MenuItems = {"spanish", "english"};
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Activity.this);
                // set title
                alertDialogBuilder.setTitle("Select");
                // set dialog message
                alertDialogBuilder
                    .setCancelable(true)
                    .setSingleChoiceItems(MenuItems, 0, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog1, int pos) {
                            // TODO Auto-generated method stub

                            if(pos == 0) {
                                 // Do Your choice
                            } else if(pos == 1) {
                                 // Do Your choice
                            }
                            dialog1.cancel();
                        }
                    })
                    .setPositiveButton("Cancel",new DialogInterface.OnClickListener() {
                        @SuppressLint("NewApi")
                        public void onClick(DialogInterface dialog1,int id) {
                            dialog1.cancel();
                        }
                      });

                    // create alert dialog
                    AlertDialog alertDialog = alertDialogBuilder.create();
                    // show it
                    alertDialog.show();
            }
        });   

答案 1 :(得分:1)

它完全可以实现Tamilan和Vipul提到的内容,但是将来如果你需要更好的控制超越警报对话框,它可以更好地使用customAlertDialog, 像往常一样创建Layput;你在你的活动中做到了。

AlertDialog dialog;
public void myDialog(){

AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Dialog Demo");
LayoutInflater inflater=getLAyoutInflater();
final View dialogView=inflater.inflate(R.layout.cutomdialog,null);
builder.setView(dialogView);
dialog=builder.create();
dialog.show();
}

<强> cutomdialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="+@id/myLayout
android:layout_width="match_parent"
android:layout_hight="match_parent"
android:orientation="vertical">

<ListView
android:id="+@id=myListView'
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<Button
android:id="+@id/myOkButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="OK"/>

</LinearLayout>

答案 2 :(得分:0)

final CharSequence[] items = {"Foo", "Bar", "Baz"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Make your selection");
builder.setItems(items, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
         // Do something with the selection
    }
});
AlertDialog alert = builder.create();
alert.show();

来源:

ListView in AlertDialog