无法在 Android 中实现自定义对话框的代码

发布于 2024-12-12 03:16:19 字数 3378 浏览 0 评论 0原文

我想在 Android 中实现自定义对话框。但是,当我自己尝试时,我无法运行我的代码。

我在 Eclipse 中的这些语句中收到错误:
1) 按钮button1main = (Button) findViewById(R.id.*btn1*);

2) TextView text = (TextView)dialog.findViewById(R.id.*txt3*);

我的 Android 代码是:

package com.example;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Main2 extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.front);

        Button button1main = (Button) findViewById(R.id.btn1);
        button1main.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Dialog dialog = new Dialog(Main2.this);
                dialog.setContentView(R.layout.maindialog);
                dialog.setTitle("This is my custom dialog box");
                dialog.setCancelable(true);

                TextView text = (TextView) dialog.findViewById(R.id.txt3);
                text.setText(R.string.lots_of_text); 

                Button button = (Button) dialog.findViewById(R.id.Button01);
                button.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        finish();
                    }
                });

                dialog.show();
            }

        });
    } 
}

这是我的 xml: front.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/RelativeLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">


  <TextView
     android:id="@+id/txt3"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="This is my main activity, from here, I want to display a dialog, after the user clicked the button below this text."/>

  <Button
      android:layout_height="wrap_content"
      android:layout_below="@+id/txt3"
      android:layout_width="wrap_content"
      android:id="@+id/btn1"
      android:text="Hey! There is more..."/>

</RelativeLayout>

maindialog.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout>

  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content" android:layout_height="wrap_content">

  <ScrollView>
    android:id="@+id/ScrollView01"
    android:layout_width="wrap_content"
    android:layout_height="200px">

    <TextView>
      android:text="@+id/txt3"
      android:layout_width="wrap_content" android:layout_height="wrap_content">
    </TextView>

  </ScrollView>


  <Button>
    android:id="@+id/Button01"
    android:layout_below="@id/ScrollView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="Cancel"
  </Button>

</RelativeLayout>

在删除上述语句中的那些红线错误之前,我无法在 Eclipse 中运行我的代码。有人可以帮助我吗?

谢谢

I want to implement Custom Dialog in Android. However , when I tried it on my own I am unable to run my code.

I am getting an error in these statements in Eclipse:
1) Button button1main = (Button) findViewById(R.id.*btn1*);

2) TextView text = (TextView) dialog.findViewById(R.id.*txt3*);

My Android code is :

package com.example;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Main2 extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.front);

        Button button1main = (Button) findViewById(R.id.btn1);
        button1main.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Dialog dialog = new Dialog(Main2.this);
                dialog.setContentView(R.layout.maindialog);
                dialog.setTitle("This is my custom dialog box");
                dialog.setCancelable(true);

                TextView text = (TextView) dialog.findViewById(R.id.txt3);
                text.setText(R.string.lots_of_text); 

                Button button = (Button) dialog.findViewById(R.id.Button01);
                button.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        finish();
                    }
                });

                dialog.show();
            }

        });
    } 
}

Here are my xml's :
front.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/RelativeLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">


  <TextView
     android:id="@+id/txt3"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="This is my main activity, from here, I want to display a dialog, after the user clicked the button below this text."/>

  <Button
      android:layout_height="wrap_content"
      android:layout_below="@+id/txt3"
      android:layout_width="wrap_content"
      android:id="@+id/btn1"
      android:text="Hey! There is more..."/>

</RelativeLayout>

maindialog.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout>

  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content" android:layout_height="wrap_content">

  <ScrollView>
    android:id="@+id/ScrollView01"
    android:layout_width="wrap_content"
    android:layout_height="200px">

    <TextView>
      android:text="@+id/txt3"
      android:layout_width="wrap_content" android:layout_height="wrap_content">
    </TextView>

  </ScrollView>


  <Button>
    android:id="@+id/Button01"
    android:layout_below="@id/ScrollView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="Cancel"
  </Button>

</RelativeLayout>

I am not able to run my code in Eclipse until I remove those red line errors in the above mentioned statements..Can someone help me ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

不顾 2024-12-19 03:16:20

您的 xml 语法有错误。

<RelativeLayout>  <- this is wrong
    android:id="@+id/RelativeLayout01" 
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android">

您需要删除“>” ,这适用于所有元素,而不仅仅是相对布局

<RelativeLayout
    android:id="@+id/RelativeLayout01" 
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android">

You have errors in your xml syntax.

<RelativeLayout>  <- this is wrong
    android:id="@+id/RelativeLayout01" 
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android">

You need to remove ">" , this applies to all elements, not only relative layout

<RelativeLayout
    android:id="@+id/RelativeLayout01" 
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文