如何在android上连接xml与类

如何在android上连接xml与类,android,Android,我创建了一个名为“new_game.xml”的新.xml文件和一个名为“new_game.java”的类 这些不是主要活动 new_game.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"

我创建了一个名为“new_game.xml”的新.xml文件和一个名为“new_game.java”的类 这些不是主要活动

new_game.xml:

<RelativeLayout 
    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"/>               
              
    <Button android:layout_width="wrap_content"                  
        android:layout_height="wrap_content"               
        android:text="Click"                 
        android:id="@+id/button"              
        android:layout_marginTop="27dp"                
        android:layout_alignRight="@+id/editText"/>              
                
    <EditText android:layout_width="wrap_content"                 
        android:layout_height="wrap_content"                
        android:inputType="textPersonName"                
        android:ems="10"                
        android:id="@+id/editText"               
        android:layout_marginTop="16dp"               
        android:layout_below="@+id/button"             
        android:layout_marginLeft="11dp"/>         
       
    <ImageView android:layout_width="wrap_content"       
        android:layout_height="wrap_content"       
        android:id="@+id/imageView"       
        android:paddingTop="50dp"      
        android:paddingBottom="50dp"      
        android:paddingLeft="50dp"      
        android:paddingRight="50dp"      
        android:background="@drawable/ic_launcher"       
        android:layout_centerVertical="true"       
        android:layout_toLeftOf="@+id/button"/>      
</RelativeLayout>  
请解释清楚

我在这个月加入了AndroidManifest.xml

/activity>
这是一个关于onClick的事件

Intent myIntent=newintent(v.getContext(),new_Game.class)

startActivityForResult(myIntent,0)


您的XML是错误的。从XML中的第一个
RelativeLayout
中删除最后一个
/

<RelativeLayout 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">  

而不是

<RelativeLayout 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"/>  


尽管Eclipse应该警告您这一点。

您的代码是正确的,但是

按钮android:layout_marginTop=“27dp”

edittext android:layout_marginTop=“16dp”

编辑文本放置在按钮上,因此当您单击时,您正在单击edittext而不是按钮将edittext marginTop更改为30dp并选中

public class New_Game extends Activity {
EditText editText1;
@override
protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);      

    setContentView(R.layout.new_game);      


     Button button1 = (Button)findViewById(R.id.button);        

    editText1 = (EditText)findViewById(R.id.editText);     

    ImageView imageView1 = (ImageView)findViewById(R.id.imageView);     

  button1.setOnClickListener(new View.OnClickListener(){

    public void onClick(View v){     

        editText1.setText("Hello Android");     
}     
  });     

 }     

在.xml上,我有一个按钮和一个editText,当我按下按钮时,我没有;不要在editText上显示消息“Hello World”。问题是我没有用xml连接这个类。你有没有收到任何错误,崩溃?到目前为止,我没有看到任何错误,除非您先单击
ImageButton
,这会给您带来问题。调用
setContentView()
时,xml已“连接”。它被称为
inflating
the
layout
,使其可见。我没有收到任何错误,但当我单击按钮时,它什么也不做。ImageButton并不重要,我现在将其删除。我认为问题在于
布局本身。你能在编辑器的图形视图中看到所有的
视图吗?因为有些属性在我看来并不正确,这就是我看到的我已经编辑了我的答案EditText应该在类级别声明,以便在onclick中访问,如果不是,它将要求您将其设置为最终值,如果是最终值,则不会更改。现在就试试上面的代码吧,即使这样也不行。我认为问题在于,在protectedvoid onCreate(Bundle savedInstanceState)之前,在classput@override的“extends Activity”上添加其他内容
<RelativeLayout 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"/>  
public class New_Game extends Activity {
EditText editText1;
@override
protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);      

    setContentView(R.layout.new_game);      


     Button button1 = (Button)findViewById(R.id.button);        

    editText1 = (EditText)findViewById(R.id.editText);     

    ImageView imageView1 = (ImageView)findViewById(R.id.imageView);     

  button1.setOnClickListener(new View.OnClickListener(){

    public void onClick(View v){     

        editText1.setText("Hello Android");     
}     
  });     

 }