Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

Java/Android, Multiple Classes

I am new to Android and I have multiple classes. I was wondering if there is a way that I could somehow include four classes in one major class.

Below is example of my code. I am looking forward to help.

This is the FindUs class (major class)

package com.abc.example;

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

public class FindUs extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.findUs);

            Button first = (Button) findViewById(R.id.first);
            Button second = (Button) findViewById(R.id.second);
            Button third = (Button) findViewById(R.id.third);
            Button fourth = (Button) findViewById(R.id.fourth);

            first.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    startActivity(new Intent("com.abc.example.FIRST"));
                }
            });

            second.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    startActivity(new Intent("ccom.abc.example.SECOND"));
                }
            });

            third.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    startActivity(new Intent("com.abc.example.THIRD"));
                }
            });

            fourth.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    startActivity(new Intent("com.abc.example.FOURTH"));
                }
            });             
}

These are the minor classes (it makes sense only to post one).

package com.abc.example;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class First extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first);

    Button callFirst= (Button) findViewById(R.id.callfirst);
    Button mapFirst= (Button) findViewById(R.id.mapfirst);

    callFirst.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String phoneNumber = "tel:+18000000000";
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse(phoneNumber));
            startActivity(callIntent);

        }
    });

    mapFirst.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String url = "grab google directions for this place";
            Intent mapIntent = new Intent(android.content.Intent.ACTION_VIEW,  Uri.parse(url));
            startActivity(mapIntent);
                }
            });             
}

So you see, I will have different numbers and location for each class. So I am unsure if I should just keep the four individual classes or if I should include all four within one major class.

Thanks. Looking forward to learning more about this from your guys.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should have every activity in a separate file.

If you have helper classes that that are only used in one activity you can use an inner class or put it below the other one (not public). Activities should be separate files.

What you could do is put code that has to be in the two classes also in a separate class so that both of them can use it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...