繁体   English   中英

具有项目列表的警报对话框

[英]alert dialog box having list of item

在我的Android应用程序中,当我单击文本视图时,我想显示一个包含项目列表的警告对话框。 这怎么可能。 善意的指导。

我把它编码为:

cus_name_txt = (TextView)findViewById(R.id.cus_name_txta);
    cus_name_txt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Onclick_click1(cus_name_txt);
            // TODO Auto-generated method stub

        }
    }); 

    contact_no_txt = (TextView)findViewById(R.id.contact_no_txta);


    attend_by_txtbx = (EditText)findViewById(R.id.attend_by_txt);
    attend_by_txtbx.setText(My_Task.attend_by_txt);


    ticket_no_txt = (TextView)findViewById(R.id.ticket_no_txta);


    task_detail_txt = (TextView)findViewById(R.id.task_detail_txt);

如何通过单击textView获取项目列表的警告框。 请指导。 我会感激你的

如果要显示progressBar在加载List in alert对话框之前,请使用AsyncTask。

例如:

private class LoadingTask extends AsyncTask<String, Void, String> {
        @Override
        protected void onPreExecute(){
               super.onPreExecute();
               progressDialog.show();
        }

        @Override
        protected String doInBackground(String... str) {
            String response = "";
                    // Call Web Service here and return response

            response = API.getDealsByCategory(str[0], str[1]); 
                    // e.g.: above is my WebService Function which returns response in string
            return response;
        }
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            System.out.println("result is: "+result);


            new Thread(new Runnable() {
                @Override
                public void run() {
                    progressDialog.dismiss();
                }
            }).start();

        // SHOW THE ALERT DIALOG HERE.....  
        }
    }

如下所示调用AsyncTask:

LoadingTask task = new LoadingTask(); task.execute( “YOUR_PARAMETER”, “YOUR_PARAMETER”);

// ==============================

只需将下面的代码放在AsyncTask的Post Excution中,您就可以获得所需的内容。

final CharSequence[] items = {"","50","100","150","200","250","300","350","400","450","500","550","600","650","700","750","800","850","900","1000"};
        AlertDialog.Builder builder = new AlertDialog.Builder(getParent());
        builder.setTitle("Select Country");
        //builder.setI
        builder.setItems(items, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int item) {
                //Toast.makeText(getApplicationContext(), con.get(item).getCountrName(), Toast.LENGTH_SHORT).show();
                selectDistanceTV.setText(items[item]);
                System.out.println("Item is: "+items[item]);
                /*CONTRY_ID = con.get(item).getCountryId();
                stateET.requestFocus();*/
           }
        });
        AlertDialog alert = builder.create();
        alert.show();

希望它会对你有所帮助。

如果您需要有关如何使用AsyncTask的更多帮助,请参阅此处: Vogella

评论我的任何查询。

享受编码...... :)

将以下代码放在onClick of textView

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Color Mode");

ListView modeList = new ListView(this);
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" };
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
modeList.setAdapter(modeAdapter);

builder.setView(modeList);
final Dialog dialog = builder.create();

dialog.show();

要么

Dialog dialog = new Dialog(**Your Context**);
  dialog.setContentView(R.layout.**Your Layout File**);
  dialog.show();

在此布局文件中,您可以根据自己的要求进行布局。 如果要从Dialog Layout文件中使用ListView,则必须编写

  ListView listView = (ListView)**dialog**.findViewById(R.id.**Your ListView Id**)

您可以传递字符串数组中的项列表并将其显示在AlertBox中。

例如:

private void SingleChoice() {  

String [] selectFruit = new String [] {“Apple”,“orange”,“mango”};

 Builder builder = new AlertDialog.Builder(this);  
 builder.setTitle("Single your Choice");  
 builder.setItems(selectFruit, new DialogInterface.OnClickListener() {  
   @Override  
   public void onClick(DialogInterface dialog, int which) {  
     Toast.makeText(MainActivity.this,  
         selectFruit[which] + " Selected", Toast.LENGTH_LONG)  
         .show();  
     dialog.dismiss();  
   }  
 });  
 builder.setNegativeButton("cancel",  
     new DialogInterface.OnClickListener() {  
       @Override  
       public void onClick(DialogInterface dialog, int which) {  
         dialog.dismiss();  
       }  
     });  
 AlertDialog alert = builder.create();  
 alert.show();  

}

button.setOnClickListener(new OnClickListener()
    {

        public void onClick(View arg0) 
        {
            new AlertDialog.Builder(MainActivity.this)

            .setSingleChoiceItems(arrClientName,0, new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int which) 
                {
                    name.setText(arrClientName[which]);


                }
            })
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() 
            {
                       public void onClick(DialogInterface dialog, int id) 
                       {

                       }
            })
            .show();

        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM