Android 问题:根 Activity 使用自定义对话框布局而不是 main.xml

发布于 2024-09-10 20:25:19 字数 3385 浏览 3 评论 0原文

我按照本指南制作自定义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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

‘画卷フ 2024-09-17 20:25:19

尝试过吗

setContentView(R.layout.main);

你在 onCreate 方法中

?如果是这样,抱歉,希望其他人可以更好地插话。

Did you try/have

setContentView(R.layout.main);

in the onCreate method?

If so, sorry, hopefully someone else can better chime in.

我很OK 2024-09-17 20:25:19

尝试删除 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文