当前位置: 动力学知识库 > 问答 > 编程问答 >

android - screen to screen command

问题描述:

Hi am new to android and have spent over 10 hours looking for this answer but i cant seem to find and understand it. I am at the main xml page. I am trying to create a button that goes to another page. I need the easiest and simplest way to do this. Can anyody please help me? Heres my code for my main 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" >

<TextView

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_centerVertical="true"

android:padding="@dimen/padding_medium"

android:text="@string/hello_world"

tools:context=".MainActivity" />

<Button

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/textView1"

android:layout_centerHorizontal="true"

android:layout_marginTop="66dp"

android:text="Button" />

</RelativeLayout>

网友答案:
<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView1" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="66dp" 
    android:text="Button" /> 

Go to your activity Initialize your button

Button btn=(Button)findViewById(R.id.button1);
// Register listener to button btn
btn.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

            // your action

            Intent newActivityIntent= new Intent(currentActivity.this, NewClass.class);
            startActivity(newActivityIntent);       
        }
});
网友答案:

You can also do it different...

You can instantiate a Button in your Activity:

private Button button;

Inside the onCreate(Bundle bundle) method you find your button, as defined at this Activity XML, which is the one you used setContentView(R.layout.yourxml);:

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

Then you use an OnClickListener:

button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             // Perform action on click
         }
     });

Inside the onClick method, you instantiate an Intent

Intent intent = new Intent(CurrentActivity.this, ActivityToGo.class);

CurrentActivity = the one you are, and ActivityToGo the one you want to load.

startActivity();

Read the basics on Android here.

网友答案:
public class MyActivity extends Activity implements OnClickListener {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(this);
    }

    public void onClick(View v) {
        // Use a switch on v.getId() if you have multiple clickable views.
        // Since there's just one button, ...
        Intent intent = new Intent(this, TargetActivity.class;
        startActivity(intent);
    }
}
网友答案:

You have to learn How to switch between Activities/Screens in Android, you can find here a well explained tutorial for beginners

网友答案:

This process is documented on the website concerning buttons. Google search Android Button

<Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="66dp"
 android:text="Button"
 android:clickable="true"
 android:onClick"insertMethodNameHere"
 />  

This will call the method you define in the onClick tag, then start a new activity or update the view. Whatever you need to do

分享给朋友:
您可能感兴趣的文章:
随机阅读: