简体   繁体   English

对话框中不显示EditText提示和TextViews

[英]EditText hints and TextViews not displaying in dialog

Would highly appreciate any help with this as I am doing this for a project that's due soon. 非常感谢您对此提供的任何帮助,因为我正在为即将到期的项目进行此操作。

I have my EditText and TextViews in a dialog box, being created in java, and I'm curious as to why nothing seems to be displaying properly... 我有一个用Java创建的对话框中的EditText和TextViews,我很好奇为什么似乎没有正确显示...

It displays fine in android studio's rendering preview: Android Studio Screenshot 它在android studio的渲染预览中显示良好: Android Studio屏幕截图

However, In the actual emulator this is what displays: Emulator Screenshot 但是,在实际的模拟器中,将显示以下内容: Emulator屏幕截图

Here is my name_entry.xml file: 这是我的name_entry.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Player 1 Name:"
    android:id="@+id/textView3" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:id="@+id/player1_name" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Player 2 Name:"
    android:id="@+id/textView4" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:enabled="true"
    android:id="@+id/player2_name" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Player 3 Name:"
    android:id="@+id/textView5" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:id="@+id/player3_name" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Player 4 Name:"
    android:id="@+id/textView6" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:id="@+id/player4_name" />

And here is my MainActivity.java: 这是我的MainActivity.java:

package me.isaac.historyquiz;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.text.Layout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    changeNames();

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

public void changeNames() {
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    LayoutInflater li = LayoutInflater.from(getApplicationContext());
    View promptsView = li.inflate(R.layout.name_entry, null);
    builder.setView(promptsView);

    final EditText p1_edit = (EditText) promptsView.findViewById(R.id.player1_name);
    final EditText p2_edit = (EditText) promptsView.findViewById(R.id.player2_name);
    final EditText p3_edit = (EditText) promptsView.findViewById(R.id.player3_name);
    final EditText p4_edit = (EditText) promptsView.findViewById(R.id.player4_name);

    builder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            String player1 = "Player 1";
            String player2 = "Player 2";
            String player3 = "Player 3";
            String player4 = "Player 4";
            String player1_name = p1_edit.getText().toString();
            String player2_name = p2_edit.getText().toString();
            String player3_name = p3_edit.getText().toString();
            String player4_name = p4_edit.getText().toString();

            if(player1_name != "") {
                player1 = player1_name;
            }
            if(player2_name != "") {
                player2 = player2_name;
            }
            if(player3_name != "") {
                player3 = player3_name;
            }
            if(player4_name != "") {
                player4 = player4_name;
            }
            startGame(player1, player2, player3, player4);
        }
    });
    builder.setTitle("Player Names");
    builder.setCancelable(false);

    AlertDialog dialog = builder.create();
    dialog.show();
}

public void startGame(String player1, String player2, String player3, String player4) {
    Toast gameStarting = Toast.makeText(getApplicationContext(), "Game Starting...", Toast.LENGTH_LONG);
    gameStarting.setGravity(Gravity.BOTTOM|Gravity.RIGHT, 4, 4);
    gameStarting.getView().setBackgroundResource(R.drawable.toast_square);
    gameStarting.show();
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}


@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.reset_game) {
        //Restart the app
        Context context = getApplicationContext();
        Intent mStartActivity = new Intent(context, MainActivity.class);
        int mPendingIntentId = 10108401;
        PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId,    mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
        System.exit(0);
    } else if(id == R.id.change_points) {
        //TODO: Change Points
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
  }
}

Use this to get layout inflater from the current Activity 使用此功能可从当前活动获取布局放大

LayoutInflater li = this.getLayoutInflater();

instead of using LayoutInflater li = LayoutInflater.from(getApplicationContext()); 而不是使用LayoutInflater li = LayoutInflater.from(getApplicationContext());

There is nothing wrong with your code. 您的代码没有错。 I have tested it as shown below. 我已经如下测试。 It might be something with your Emulator. 您的仿真器可能有问题。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM