我在我的活动中调用方法ShowDialog。我有一个名单列表,所以我将在这个名单列表中循环,对于每个名字,我会得到它的数字。有些名称可能只有一个数字,有些名称可能有更多。如果名称有多个号码,我会显示另一个对话框,提示您选择所需的号码。
当所有名字只有一个数字时,很简单,我调用dialog.dismiss,然后调用SendSMS(listOfNumbers),最后完成():
ShowDialog();
SendSMS(listOfNumbers);
finish();
但是,当用户有多个号码时会出现问题。我不能找到办法在流程结束时通知我。
ShowDialog()方法:
protected void ShowDialog() {
final Dialog dialog = new Dialog(ContactsPicker.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.alert_dialog);
final CheckBox cbE = (CheckBox) dialog.findViewById(R.id.checkBoxEmail);
final CheckBox cbN = (CheckBox) dialog.findViewById(R.id.checkBoxNumber);
Button button = (Button) dialog.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String names = "";
String names1 = "";
String names2 = "";
if (cbN.isChecked()) {
//Send invitations through SMS
//List<String> numbers = new ArrayList<String>();
String number = "";
List<String> usernumbers = new ArrayList<String>();
//Fill names string to send it back
for (int i = 0; i < selected.size(); i++) {
//number = get_Number(contactName, getApplicationContext());
usernumbers = getNumber(selected.get(i), getApplicationContext());
//Toast.makeText(getApplicationContext(),"Usernumbers list size: "+usernumbers.size(),Toast.LENGTH_SHORT).show();
if (usernumbers.size() == 1) {
selectedNumbers.add(usernumbers.get(0));
} else if (usernumbers.size() > 1) {
//Show select number dialog
dialog.hide();
showSelectNumberPopup(usernumbers);
} else {
}
//dialog.show();
}
}
if (cbE.isChecked()) {
//Send invitations through e-mail
List<String> emails = new ArrayList<String>();
String email = "";
for (String contactName : selected) {
email = get_Email(contactName, getApplicationContext());
emails.add(email);
names2 += contactName + ",";
}
//showToast("Send E-mail Checked");
sendEmail(emails);
}
}
});
}
showSelectNumberPopup方法:
protected void showSelectNumberPopup(List<String> usernumbers) {
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.alert_dialog_select_number, null);
// vi = LayoutInflater.from(getBaseContext()).inflate(R.layout.alert_dialog_select_number,null);
//View view = (View)getLayoutInflater().inflate(R.layout.alert_dialog_select_number, null);
dialogLV = (ListView) view.findViewById(R.id.list_select_number);
dialogLV.setAdapter(new SelectNumberDialogListAdapter(getApplicationContext(), usernumbers));
dialogLV.setItemsCanFocus(false);
dialogLV.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
dialogLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
String selectedFromList = (String) (dialogLV.getItemAtPosition(myItemInt));
cbSelectedNumber = (CheckBox) myView.findViewById(R.id.checkBoxNumber);
if (selectedNumbers.contains(selectedFromList)) {
selectedItemCB.setChecked(false);
selectedNumbers.remove(selectedFromList);
//Toast.makeText(getApplicationContext(), "Removed From SELECTED: " + selectedFromList, Toast.LENGTH_SHORT).show();
} else {
selectedItemCB.setChecked(true);
selectedNumbers.add(selectedFromList);
//Toast.makeText(getApplicationContext(), "Added To SELECTED: " + selectedFromList, Toast.LENGTH_SHORT).show();
}
}
});
final Dialog dialog = new Dialog(ContactsPicker.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(view);
//Toast.makeText(getApplicationContext(),"GOT IN THE SECOND POPUP",Toast.LENGTH_SHORT).show();
Button button = (Button) dialog.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
if (names1 != "")
names = names1;
else if (names2 != "")
names = names2;
else {
for (String contactName : selected) {
names += contactName + ",";
}
}
names = names.substring(0, names.lastIndexOf(","));
//Set data to send it back to PickNamesActivity
Intent data = new Intent();
//---set the data to pass back---
data.setData(Uri.parse(names));
setResult(RESULT_OK, data);
//finish();
//sendSMS(selectedNumbers);
//finish();
dialog.show();
}
答案 0 :(得分:0)
TL; DR:Use an interface
最简单的方法是创建两个(内部)Dialog类,扩展DialogFragment并使用new FirstDialog().show()
调用它们。将关联的代码移动到这些对话框。例如:
private class FirstDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View view = getLayoutInflater().inflate(R.layout.alert_dialog, null);
final CheckBox cbE = (CheckBox)view.findViewById(R.id.checkBoxEmail);
final CheckBox cbN = (CheckBox)view.findViewById(R.id.checkBoxNumber);
return new AlertDialog.Builder(this)
.setPositiveButton("Text here", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Put your on click stuff here
}
}).create();
}
}
private class SecondDialog extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View view = getLayoutInflater().inflate(R.layout.alert_dialog_select_number, null);
dialogLV = (ListView)view.findViewById(R.id.list_select_number);
dialogLV.setAdapter(new SelectNumberDialogListAdapter(getApplicationContext(),usernumbers));
dialogLV.setItemsCanFocus(false);
dialogLV.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
dialogLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
String selectedFromList = (String) (dialogLV.getItemAtPosition(myItemInt));
cbSelectedNumber = (CheckBox)myView.findViewById(R.id.checkBoxNumber);
if (selectedNumbers.contains(selectedFromList)) {
selectedItemCB.setChecked(false);
selectedNumbers.remove(selectedFromList);
//Toast.makeText(getApplicationContext(), "Removed From SELECTED: " + selectedFromList, Toast.LENGTH_SHORT).show();
} else {
selectedItemCB.setChecked(true);
selectedNumbers.add(selectedFromList);
//Toast.makeText(getApplicationContext(), "Added To SELECTED: " + selectedFromList, Toast.LENGTH_SHORT).show();
}
}
});
return new AlertDialog.Builder(this)
.setView(view)
.setPositiveButton("Text here", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//What happens when the button is clicked
}
}).create();
}
不确定你的所有代码是做什么的,或者你的对话框是如何设置的,但希望有了这个基础,你可以适应其余的东西。当你想从第二个对话框中获取某些内容时,你可以为它添加一个接口,在你的Activity里面调用一个方法,它可以做任何需要做的事情。 Here's an Android Developer link to help you with that.