1

所以我弹出了这个对话框。

但是,这些按钮没有响应。未达到 onClick 方法内的调试行。

我做错了什么或没有做什么?

这是我的代码,然后是对话框的 XML。

    lv = new ListView(this);
    lv.setAdapter(aa);
    lv.setId(getTaskId());
    setContentView(lv);

    lv.setOnItemClickListener(new  OnItemClickListener() {         
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int pos,
                long id) {
             TextView v=(TextView) view.findViewById(R.id.info);
             String str = (String) v.getText();
             String ssid = str.substring((str.indexOf(" ")+1), str.indexOf(newline));
             AlertDialog.Builder b = new AlertDialog.Builder(context);
             b.setView(getLayoutInflater().inflate(R.layout.loginbox,(ViewGroup) view));
             b.setPositiveButton(R.id.loginbutton, new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {
                      Log.i("Button","Log in");
                   }
               });
               b.setNegativeButton(R.id.cancelbutton, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       Log.i("Button","Cancel");
                       dialog.dismiss();
                   }
               });

             Log.i("Click",ssid);
        }       
        });

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:orientation="horizontal" >

<EditText
    android:id="@+id/username"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="4dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginTop="16dp"
    android:hint="@string/ssidlabel"
    android:inputType="textEmailAddress"
    android:text="@string/ssidlabel" />

<EditText
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/username"
    android:layout_marginTop="14dp"
    android:ems="10"
    android:fontFamily="sans-serif"
    android:hint="@string/password"
    android:inputType="textPassword"
    android:text="@string/password" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/loginbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/username"
    android:layout_below="@+id/password"
    android:text="@string/login" />

<Button
    android:id="@+id/cancelbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/loginbutton"
    android:layout_alignBottom="@+id/loginbutton"
    android:layout_alignRight="@+id/username"
    android:text="@string/cancel" />

4

2 回答 2

1
         TextView v=(TextView) view.findViewById(R.id.info);
         String str = (String) v.getText();
         String ssid = str.substring((str.indexOf(" ")+1), str.indexOf(newline));
         AlertDialog.Builder b = new AlertDialog.Builder(context);
         b.setView(getLayoutInflater().inflate(R.layout.loginbox, null, false));
         b.setPositiveButton(R.id.loginbutton, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                  Log.i("Button","Log in");
               }
           });
           b.setNegativeButton(R.id.cancelbutton, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   Log.i("Button","Cancel");
                   dialog.dismiss();
               }
           });

         AlertDialog alertDialog = b.create();
         alertDialog.show();

         Log.i("Click",ssid);
于 2013-05-03T20:24:30.977 回答
0

您可以尝试以下方法:

lv = new ListView(this);
lv.setAdapter(aa);
lv.setId(getTaskId());
setContentView(lv);

lv.setOnItemClickListener(new  OnItemClickListener() {         
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int pos,
            long id) {
         TextView v=(TextView) view.findViewById(R.id.info);
         String str = (String) v.getText();
         String ssid = str.substring((str.indexOf(" ")+1), str.indexOf(newline));
         AlertDialog.Builder b = new AlertDialog.Builder(context);
         b.setView(getLayoutInflater().inflate(R.layout.loginbox,(ViewGroup) view));
         b.setPositiveButton("Login", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                  Log.i("Button","Log in");
               }
           });
           b.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   Log.i("Button","Cancel");
                   dialog.dismiss();
               }
           });

         b.show();
         Log.i("Click",ssid);
    }       
    });

然后你可以<Button>从你的 xml 中删除你的 s。和将为您创建按钮.setPositiveButton.setNegativeButton无需在 xml 中创建它们并使用R.id.

于 2013-05-03T20:01:02.723 回答