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

EditText hints and TextViews not displaying in dialog

非常感谢任何帮助,因为我正在为一个即将到期的项目做这件事。

我在对话框中有我的 EditText 和 TextView,它们是在 java 中创建的,我很好奇为什么似乎没有任何内容正确显示...

它在 android studio 的渲染预览中显示良好: Android Studio Screenshot

但是,在实际的模拟器中,显示的是: Emulator Screenshot

这是我的 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" />

这是我的 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;
  }
}

使用它从当前 Activity

获取布局 inflater

LayoutInflater li = this.getLayoutInflater();

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

您的代码没有任何问题。我已经对其进行了测试,如下所示。这可能与您的模拟器有关。