有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java GUI在我的应用程序中出现两次

我正在编写我的第一个安卓应用程序,并最终成功地让我的程序出现在AVD上。然而,GUI似乎在同一个窗口中出现了两次。第一个在另一个之上

看起来是这样的: http://i.stack.imgur.com/0xK1S.png

我的主要活动如下:

    <LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:id="@+id/container"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:background="@color/dark_gray"
    安卓:orientation="vertical"
    安卓:paddingBottom="@dimen/activity_vertical_margin"
    安卓:paddingLeft="@dimen/activity_horizontal_margin"
    安卓:paddingRight="@dimen/activity_horizontal_margin"
    安卓:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.xsiand.something.StartingPoint$PlaceholderFragment" >


    <LinearLayout
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_marginTop="10dp"
        安卓:background="@color/red"
        安卓:orientation="horizontal" 
        >


        <TextView
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:layout_gravity="left"
            安卓:paddingRight="2dp"
            安卓:text="Your name:"
            安卓:textColor="@color/white"
            安卓:textSize="30dp" />

        <EditText
            安卓:id="@+id/nameField"
            安卓:layout_width="120dp"
            安卓:layout_height="wrap_content"
            安卓:layout_gravity="right"
            安卓:ems="10"
            安卓:inputType="textPersonName"
            安卓:textColor="@color/white" />
    </LinearLayout>


    <Button
        安卓:id="@+id/saveButton"
        安卓:layout_width="145dp"
        安卓:layout_height="wrap_content"
        安卓:layout_gravity="center"
        安卓:layout_marginBottom="15dp"
        安卓:layout_marginTop="15dp"
        安卓:text="@string/save" />

    <TextView
        安卓:id="@+id/textResult"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:text="Your name is: "
        安卓:textColor="@color/white"
        安卓:textSize="30dp" />

</LinearLayout>

我唯一一门课是这样的:

import 安卓.app.Activity;
import 安卓.app.Fragment;
import 安卓.content.Context;
import 安卓.os.Bundle;
import 安卓.view.LayoutInflater;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.view.inputmethod.InputMethodManager;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.TextView;

public class StartingPoint extends Activity {

    private String name;
    private Button saveButton;
    private TextView display;
    private EditText nameInput;



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

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
        }

        name = null;
        nameInput = (EditText)findViewById(R.id.nameField);
        saveButton = (Button)findViewById(R.id.saveButton);  

        display = (TextView)findViewById(R.id.textResult);


        nameInput.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                //also what to put here in order to get the keyboard to appear?

            }
        });

        saveButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                if(nameInput.getText().toString() != null){
                    name = nameInput.getText().toString();
                    display.setText("Your name is: " + name);
                }
                else
                    display.setText("noname");
            }
        });

    }

@Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.starting_point, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.main, container, false);
            return rootView;
        }
    }

}

最后,我的清单是这样的:

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

    <uses-sdk
        安卓:minSdkVersion="14"
        安卓:targetSdkVersion="19" />

    <application
        安卓:allowBackup="true"
        安卓:icon="@drawable/ic_launcher"
        安卓:label="@string/app_name"
        安卓:theme="@style/AppTheme" >
        <activity
            安卓:name=".StartingPoint"
            安卓:label="@string/app_name" >
            <intent-filter>
                <action 安卓:name="安卓.intent.action.MAIN" />

                <category 安卓:name="安卓.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

为什么gui会出现两次


共 (1) 个答案

  1. # 1 楼答案

    您正在活动中添加占位符片段

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
    }
    

    而占位符片段正在膨胀与您的活动相同的布局

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.main, container, false);
        return rootView;
    }