Android 问题:根 Activity 使用自定义对话框布局而不是 main.xml
我按照本指南制作自定义AlertDialog。我想制作一个带有按钮和标题(如 AlertDialog)的对话框,但也带有 EditText 框,这是常规 AlertDialog 无法使用的。因此,我在 res/layout 中名为 add_player_dialog.xml 的文件中制作了以下自定义布局,其中包含 EditText 框,该布局将由 AlertDialog.Builder 的 setView() 调用:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/add_player_dialog_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<EditText
android:id="@+id/player_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:textSize="20sp"
/>
</LinearLayout>
然后在启动对话框中,我在 onCreateDialog() 的开关中有以下内容:
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.add_player_dialog,
(ViewGroup) findViewById(R.id.add_player_dialog_layout));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Enter player's name")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(getApplicationContext(), "yes", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
})
.setView(layout);
AlertDialog alert = builder.create();
问题是应用程序启动时首次使用的活动使用 add_player_dialog.xml 作为其布局,而不是 main.xml。我该如何避免这种情况?
=================
编辑:我找到了一种编程方法来完成此任务,如果有人感兴趣的话:
import android.R.drawable;
EditText et = new EditText(this);
et.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
et.setBackgroundResource(drawable.editbox_background);
et.setTextSize(20);
================ =
如果您有兴趣,这里是 main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="40dip"
>
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center"
>
<Button
android:id="@+id/new_game_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/new_game_button"
/>
<Button
android:id="@+id/continue_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/continue_button"
/>
</LinearLayout>
</LinearLayout>
I was following this guide to make a custom AlertDialog. I want to make a dialog with buttons and a title like an AlertDialog, but also with an EditText box, which a regular AlertDialog can't use. So I made the following custom layout containing the EditText box in a file called add_player_dialog.xml in res/layout, which will be called by AlertDialog.Builder's setView():
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/add_player_dialog_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<EditText
android:id="@+id/player_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:textSize="20sp"
/>
</LinearLayout>
Then in the activity (not the root activity, btw) that starts the dialog, I have the following in a switch in onCreateDialog():
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.add_player_dialog,
(ViewGroup) findViewById(R.id.add_player_dialog_layout));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Enter player's name")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(getApplicationContext(), "yes", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
})
.setView(layout);
AlertDialog alert = builder.create();
The problem is that the activity that is first used when the application is launched uses add_player_dialog.xml for its layout, not main.xml. How do I avoid this?
=================
EDIT: I have found a programmatic way to accomplish this, if anyone is interested:
import android.R.drawable;
EditText et = new EditText(this);
et.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
et.setBackgroundResource(drawable.editbox_background);
et.setTextSize(20);
=================
If you're interested, here is main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="40dip"
>
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center"
>
<Button
android:id="@+id/new_game_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/new_game_button"
/>
<Button
android:id="@+id/continue_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/continue_button"
/>
</LinearLayout>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试过吗
你在 onCreate 方法中
?如果是这样,抱歉,希望其他人可以更好地插话。
Did you try/have
in the onCreate method?
If so, sorry, hopefully someone else can better chime in.
尝试删除 gen/R.java 并让构建系统重新生成它,我发现有时当您添加 ID 时,它会不同步,需要强制重新生成。
Try deleting gen/R.java and letting the build system regenerate it, I have seen that sometimes when you add IDs it gets out of sync and needs to be forcefully regenerated.