所以我弹出了这个对话框。
然而,按钮没有响应。无法访问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" />
答案 0 :(得分: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);
答案 1 :(得分: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);
}
});
然后您可以从xml中删除<Button>
。 .setPositiveButton
和.setNegativeButton
会为您创建按钮,无需在xml中创建按钮,并使用R.id
引用它们。