当前位置: 首页 > 知识库问答 >
问题:

Android-主页/本机屏幕上的弹出对话框

蒋斯伯
2023-03-14

我是新手。我知道这个问题已经被问了很多次了,但是到目前为止还没有一个完整的解决方案。我首先开始运行一些没有GUI的代码,通过简单地设置主题,这是成功的。现在,我想添加对话框显示错误从代码,但我不能得到它显示!

<activity android:name="PopupActivity"
  android:launchMode="singleInstance" android:excludeFromRecents="true"
  android:taskAffinity="" android:theme="@android:style/Theme.Dialog" />

因此我创建了一个空视图,其中wrap_content作为宽度和高度,但我不确定应该为活动中的AlertDialog对象使用什么view元素。我在另一个活动中测试了所有的代码,所有的工作都很好,AlertDialog显示没有问题。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:gravity="center_horizontal">
</LinearLayout>
AlertDialog.Builder alertDialogB = new AlertDialog.Builder(cont);
alertDialogB.setTitle(title);
alertDialogB.setMessage(msg);
...
...

共有1个答案

朱修真
2023-03-14

您可以创建简单的对话与不同的解决方案,但我使用这一开始。

在这里我添加了一个带有代码的文件列表来澄清你的疑惑:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.demo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.demo.LoginActivity"
            android:label="@string/app_name"
            android:launchMode="singleInstance">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.demo.HomeActivity"
            android:label="@string/title_activity_home" 
            android:theme="@android:style/Theme.Dialog">
        </activity>
    </application>

</manifest>
package com.demo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;

public class LoginActivity extends Activity {

    Button login_btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        login_btn=(Button) findViewById(R.id.btnlogin);
        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(getApplicationContext(),HomeActivity.class);
                startActivity(i);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_login, menu);
        return true;
    }

}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".LoginActivity" 
    android:layout_gravity="center">

    <Button
        android:id="@+id/btnlogin"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"/>

</LinearLayout>
package com.demo;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class HomeActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_home, menu);
        return true;
    }

}

activity_home.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeActivity" 
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
 类似资料:
  • 我正在开发一个广播接收器的来电在Android和收到来电我想在本机进线量屏幕上膨胀弹出。 我完成了那个代码。但是现在的问题是,在Android 4.1(Jelly Bean)API级别17中,当电话铃声响起时,以的形式出现,如果我调用一个活动,它会被调用,但是下面的代码它不会被处决。我列出的代码: 我所说的活动: 之后 该代码没有在Android 4.1(Jelly Bean)中执行,但在其他版本

  • 有人知道xml主题/样式中的哪个参数负责PreferenceScreen弹出窗口中文本的颜色吗?要说明: 文本颜色是黑色的,所以我尝试的样式属性没有触及它。这个背景是白色的,但是稍后我想把它改成深色,我也需要改变文本的颜色。 首选项屏幕片段。 我分配给首选项屏幕的样式。之后

  • 本文向大家介绍Android Animation实战之屏幕底部弹出PopupWindow,包括了Android Animation实战之屏幕底部弹出PopupWindow的使用技巧和注意事项,需要的朋友参考一下 Android动画的一个实战内容,从屏幕底部滑动弹出PopupWindow。 相信这种效果大家在很多APP上都遇到过,比如需要拍照或者从SD卡选择图片,再比如需要分享某些东西时,大多会采用

  • 问题内容: 如何将通过javascript 函数打开的弹出窗口居中显示在屏幕变量中心,以当前选定的屏幕分辨率为中心? 问题答案: 更新:它现在也可以在尚未超出屏幕宽度和高度的窗口上运行! 如果您使用双显示器,则窗口将水平居中,而不是垂直居中…使用此功能可以解决此问题。 用法示例:

  • 我有一个应用程序小部件,当它更新时,获取具有与小部件匹配的维度的图像,并将该图像放入(通过)。它工作得很好。 但是对于支持主屏幕旋转的设备(我不是说基于设备方向的

  • 我在 Libgdx 游戏上的触摸接收器有问题。 在我的玩家主页上,我有这个: 当我运行我的游戏时,除了触摸监听器有时工作正常,但是在触摸几秒钟后。 Gdx.app.log(“已点击”、“已触摸”);通过触摸发射。 但是Gdx.app.log("点击了","很棒");或者Gdx.app.log("Clicked "," nooooooooooo ");我必须执行长按,直到触摸被触发。 谢谢 我添加了