开发者

Linking xml pages with layout

Excuse the simplicity of this request - but is there a way for me t开发者_如何学JAVAo link onClick commands for buttons through the Layout rather than code. I am trying to create a simple app and I want to be able to make buttons and have clicks go from one "page" to the other.

Is there a site that might overview how to use the UI to code for droid?

Thanks!


  1. Make how many ever buttons that you would like to have. Make sure if its more than the screen amount you must assign a scroll view.

assume that you have on your R.layout.main.xml. To find this go to res/layout/main.xml Copy XML, rightclick and paste it in the layout folder. Then it will say Rename. Rename it to what ever you want.

Now copy this specifically so lets say you put

btn1
btn2 
btn3
btn4 
btn5 

To make the id tag go to the properties on each button and scroll to id and rename the ending to btn1 , 2,3, 4, and so on.

Now, if you were to place that in your .java file under src/com.whateveryounamed.app/what ever you named .java

Place this code below and it will work. Below is a example of mine with 5 buttons in xml page.

package com.nashvillekurds.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class kurdishhistoryapp extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn1 =(Button)findViewById(R.id.btn1);
        Button btn2 =(Button)findViewById(R.id.btn2);
        Button btn3 =(Button)findViewById(R.id.btn3);
        Button btn4 =(Button)findViewById(R.id.btn4);
        Button btn5 =(Button)findViewById(R.id.btn5);

        btn1.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent();

                myIntent.setAction(Intent.ACTION_VIEW);

                myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
                setContentView(R.layout.btn1);

            }
        });

        btn2.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent();

                myIntent.setAction(Intent.ACTION_VIEW);

                myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
                setContentView(R.layout.btn2);

            }
        });


        btn3.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent();

                myIntent.setAction(Intent.ACTION_VIEW);

                myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
                setContentView(R.layout.btn3);

            }
        });


        btn4.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent();

                myIntent.setAction(Intent.ACTION_VIEW);

                myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
                setContentView(R.layout.btn4);

            }
        });


        btn5.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent();

                myIntent.setAction(Intent.ACTION_VIEW);

                myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
                setContentView(R.layout.btn5);

            }
        });


}}

hope this helped if not sorry but your doing something wrong ...


What james newton said involves multiple buttons. this is how it goes with one button, just for clarity:

    Button btn1 =(Button)findViewById(R.id.~btn1~);

btn1.setOnClickListener(new Button.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent myIntent = new Intent();

        myIntent.setAction(Intent.ACTION_VIEW);

        myIntent.setData(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        setContentView(R.layout.~btn1~);

    }        
    });

Besides that nothing needs to be changed, just change the button id and xml id to work with your app. Button btn is just to label it to the system, so you should be keep that, and btn1.setOnClickListener... can be kept as well. Just change the parts within the ~ . Make sure to change the ~ too! I probably wouldn't without a warning, and some others wouldn't either.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜