2015-06-10 24 views
0

我沿着一個按鈕的行做了一些事情,這些按鈕會產生隨機數字,以便與屏幕上的按鈕位移導致座標互換,但是我不知道如何替換生成的平面X和Y的數量。幫助,而這些就是我的應用程序的代碼,只是我教的,從來沒有找到足夠的支持我的知識狀態。如何更改「layout_margintop」?

從波蘭

<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" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.reflex.MainActivity" > 

    <Button 
     android:id="@+id/b1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="30dp" 
     android:layout_marginTop="45dp" 
     android:text="Reflex!" /> 

</RelativeLayout> 


    package com.example.reflex; 





    import java.util.Random; 
    import android.support.v7.app.ActionBarActivity; 
    import android.annotation.SuppressLint; 
    import android.content.DialogInterface; 
    import android.content.DialogInterface.OnClickListener; 
    import android.os.Bundle; 
    import android.view.Menu; 
    import android.view.MenuItem; 
    import android.view.View; 
    import android.widget.Button; 


    public class MainActivity extends ActionBarActivity implements android.view.View.OnClickListener 

    { 

    private Button generuj; 
    private int wynik1; 
    private int wynik2; 
    private final static int min = 1; 
    private final static int maxh = 109; 
    private final static int maxv = 239; 
    private Random randomGenerator; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 

    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     generuj = (Button) findViewById(R.id.b1); 
     randomGenerator = new Random(); 




    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 

    { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, 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); 
    } 


    @SuppressLint("NewApi") @Override 
    public void onClick(View v) 
    { 
     if(v.getId() == R.id.b1) 
     { 
      int temp = randomGenerator.nextInt(maxh) + min; 
      int temp1 = randomGenerator.nextInt(maxv) + min; 
      generuj.setX(temp1); 
      generuj.setY(temp); 

     } 

    } 
} 

回答

0

翻譯由谷歌您需要在點擊事件添加到您的XML。

android:onclick="onClick" 

在按鈕標籤

+0

現在我有錯誤:沒有在包 '機器人' \t activity_main.xml中\t /反射發現屬性的 'onClick' 資源標識符/ RES /佈局\t線11 \t的Android! AAPT問題 –