当用户想要添加新项目时,我调用了一个侦听器, 监听器应该以这种形式显示带有2个EditText的AlertDialog
-AlertDialog Title
-AlertDialog Message
-TextView 1 above EditText1
-EditText1
-TextView 2 above EditText2
-EditText 2
两个按钮
-*Cancel* on the left
-*Add* on the right
我的代码是
OnClickListener addNewItemListener = new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(
MyActivity.this);
alert.setTitle(R.string.add_title);
alert.setMessage(R.string.add_message);
final TextView t1 = new TextView(MyActivity.this);
t1.setText("Name");
final EditText input1 = new EditText(MyActivity.this);
final TextView t2 = new TextView(MyActivity.this);
t2.setText("Value");
final EditText input2 = new EditText(MyActivity.this);
alert.setView(t1);
alert.setView(input1);
alert.setView(t2);
alert.setView(input2);
alert.setPositiveButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
});
alert.setNegativeButton(R.string.add,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
try {
String name = input1.getText().toString();
double value = Double.parseDouble(input2
.getText().toString());
addItem(name, value);
} catch (RuntimeException e) {
Alerts.DatiErrati(MyActivity.this);
}
}
});
alert.show();
}
};
但不幸的是,我得到了一个只有一个EditText和两个按钮的AlertDialog,尽管我的原意是
我该如何解决这个问题?
答案 0 :(得分:1)
您必须在LinearLayout中包含多个小部件,并使用setView进行最终布局
试试这个
OnClickListener addNewItemListener = new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(
MyActivity.this);
LinearLayout myLayout= new LinearLayout(MyActivity.this);
myLayout.setOrientation(LinearLayout.VERTICAL);
alert.setTitle(R.string.add_title);
alert.setMessage(R.string.add_message);
final TextView t1 = new TextView(MyActivity.this);
t1.setText("Name");
final EditText input1 = new EditText(MyActivity.this);
final TextView t2 = new TextView(MyActivity.this);
t2.setText("Value");
final EditText input2 = new EditText(MyActivity.this);
myLayout.addView(t1);
myLayout.addView(input1);
myLayout.addView(t2);
myLayout.addView(input2);
alert.setView(myLayout);
alert.setPositiveButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
});
alert.setNegativeButton(R.string.add,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
try {
String name = input1.getText().toString();
double value = Double.parseDouble(input2
.getText().toString());
addItem(name, value);
} catch (RuntimeException e) {
Alerts.DatiErrati(MyActivity.this);
}
}
});
alert.show();
}
};
答案 1 :(得分:1)
您可以通过创建AlertDialog
来显示custom dialog
例如
这里是对话框 myalertdialog.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">
<TextView
android:id="+@id/myTextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView1"/>
<EditText
android:id="+@id=myeditText1'
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="+@id/myTextView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView2"/>
<EditText
android:id="+@id=myeditText2'
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
然后在你的代码中
AlertDialog dialog;
AlertDialog.Builder builder=new AlertDialog.Builder(this);
LayoutInflator inflater=getLayoutInflator();
View dialogView=inflator.inflate(R.layougt.myalertdialog,(ViewGroup) getCurrentFocus());
dialogView.setView(dialogView);
alert.setPositiveButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
});
alert.setNegativeButton(R.string.add,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
});
dialog=builder.create();
dialog.show();
答案 2 :(得分:1)
浏览链接here
XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/edit_name"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:orientation="vertical" >
<TextView
android:id="@+id/lbl_your_name" android:text="Your name"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<EditText
android:id="@+id/txt_your_name"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:inputType=”text”
android:imeOptions="actionDone" />
</LinearLayout>
java文件
public class EditNameDialog extends DialogFragment implements OnEditorActionListener {
public interface EditNameDialogListener {
void onFinishEditDialog(String inputText);
}
private EditText mEditText;
public EditNameDialog() {
// Empty constructor required for DialogFragment
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_edit_name, container);
mEditText = (EditText) view.findViewById(R.id.txt_your_name);
getDialog().setTitle("Hello");
// Show soft keyboard automatically
mEditText.requestFocus();
getDialog().getWindow().setSoftInputMode(
LayoutParams.SOFT_INPUT_STATE_VISIBLE);
mEditText.setOnEditorActionListener(this);
return view;
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (EditorInfo.IME_ACTION_DONE == actionId) {
// Return input text to activity
EditNameDialogListener activity = (EditNameDialogListener) getActivity();
activity.onFinishEditDialog(mEditText.getText().toString());
this.dismiss();
return true;
}
return false;
}
}
答案 3 :(得分:0)
为什么不为警报制作单独的xml布局...问题出在alert.setView(view); setView(view)和addView(view);
之间存在差异