非常感谢任何帮助,因为我正在为即将到期的项目做这件事。
我在一个对话框中有我的EditText和TextViews,用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;
}
}