我无法在AlertDialog中看到“我的数据”,因为它与EditText重叠。
Context context = MyActivity.this;
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle(" NEW TITLE");
alert.setMessage("MESSAGE 1");
final TextView tx = new TextView(this);
tx.setText("MY DATA");
alert.setView(tx);
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
}
});
alert.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked Cancel so do some stuff */
}
});
alert.create();
alert.show();
答案 0 :(得分:5)
试试这个:
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(Vertical);
final TextView tx = new TextView(this);
tx.setText("MY DATA");
layout.addView(tx);
// Set an EditText view to get user input
final EditText input = new EditText(this);
layout.addView(input);
alert.setView(layout);
答案 1 :(得分:1)
您只能使用setView
设置一个视图,因此不会显示“我的数据” - 因为它被EditText覆盖,所以根本不存在。按照您调用setView
的顺序切换,看看我的意思。
要将多个视图作为自定义视图,您需要创建一个ViewGroup,例如LinearLeayout,将EditText和TextView添加到该组,然后将该组设置为您的自定义对话框视图。
// Create a ViewGroup to hold the other views
LinearLayout group = new LinearLayout(this);
group.setOrientation(LinearLayout.VERTICAL);
// Header for the TextView
final TextView txHeader = new TextView(this);
txHeader.setText("TEXTVIEW HEADER");
group.addView(txHeader);
// Add the textview to the group
final TextView tx = new TextView(this);
tx.setText("MY DATA");
group.addView(tx);
// Header for the EditText
final TextView inputHeader = new TextView(this);
inputHeader.setText("EDIT-TEXT HEADER");
group.addView(inputHeader);
final EditText input = new EditText(this);
group.addView(input);
// Set the group as the custom dialog view
alert.setView(group);
答案 2 :(得分:0)
我建议使用自定义对话框,定义xml布局,例如:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/plausi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@color/tree_normal_back"
android:padding="3dip">
<TextView
android:id="@+id/plausi_tv"
android:layout_width="fill_parent"
android:textSize="22dip"
android:padding="2dip"
android:layout_height="wrap_content"
android:background="@color/tree_normal_back"
android:textColor="@color/tree_normal_font"
android:minLines="4"/>
<EditText
android:layout_below="@+id/plausi_tv"
android:id="@+id/plausi_et"
android:layout_width="fill_parent"
android:textSize="22dip"
android:padding="2dip"
android:layout_height="wrap_content"
android:textColor="@color/tree_input_font"/>
</RelativeLayout>
并构建如下对话框:
LayoutInflater mInflater = LayoutInflater.from(tab3.this);
AlertDialog.Builder builder = new AlertDialog.Builder(tab3.this);
builder.setTitle("Plausibilitätscheck");
View convertView = mInflater.inflate(R.xml.plausi, null);
RelativeLayout rl = (RelativeLayout) convertView.findViewById(R.id.plausi);
final EditText et = (EditText) convertView.findViewById(R.id.plausi_et);
final TextView tv = (TextView) convertView.findViewById(R.id.plausi_tv);
tv.setText(Html.fromHtml(vorgabe));
et.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
et.setText(GlobalVars.form_adapter.DATA[clicked_index]);
et.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0,
int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0,
int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
});
builder.setView(rl);
final AlertDialog alert = builder.create();
et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
alert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
alert.show();
答案 3 :(得分:0)
试试这段代码
“LayoutInflater factory = LayoutInflater.from(nextScreen.this);
final查看textEntryView = factory.inflate(R.layout.text_entry,null);
最终AlertDialog.Builder meeting = new AlertDialog.Builder(nextScreen.this);
meeting.setView(textEntryView).setTitle( “OK”);
meeting.setPositiveButton(“Create”,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
TextView txt=(TextView) textEntryView.findViewById(R.id.username_edit);
Toast.makeText(nextScreen.this, txt.getText().toString(), Toast.LENGTH_SHORT);
}
});
meeting.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
meeting.create();
meeting.show();"
以及.xml文件
<EditText
android:id="@+id/username_edit"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:capitalize="none"
android:gravity="fill_horizontal"
android:singleLine="true"/>