我该如何开始另一个活动被点击main.xml中定义的按钮时,我该、按钮、定义、xml

2023-09-12 06:03:27 作者:泪代替你吻我的脸

我有两个班。一个是主,另一个是为HelloWorld。当我点击它在main.xml中定义的按钮,我希望它显示在HelloWorld类中定义启动的消息。当我点击该按钮,它不会做任何事情。code如下。如果什么都需要忍受让我知道。谢谢

I have two classes. One is Main and the other is HelloWorld. When I click a button which is defined in main.xml I want it to display a message defined in HelloWorld class to start. When i click the button it does not do anything.Code is given below. If anything else is required to put up let me know. Thanks

Main.java

Main.java

public class Main extends MapActivity implements LocationListener {
/** Called when the activity is first created. */
MapView map;
long start;
long stop;
int x, y;
GeoPoint touchedPoint;
Drawable d;
List<Overlay> overlayList;
LocationManager lm;
String towers;
int lat ;
int longi; 





@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

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

    orderButton.setOnClickListener(new View.OnClickListener() {

      public void onClick(View view) {
        Intent intent = new Intent(Main.this, HelloWorld.class);
        startActivity(intent);
      };
    });

    map = (MapView)findViewById(R.id.mv);
    map.setBuiltInZoomControls(true);

    Touchy t = new Touchy();
    overlayList = map.getOverlays();
    overlayList.add(t);



    d = getResources().getDrawable(R.drawable.pinn);



    //Placing pintpoint
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria crit = new Criteria();

    towers = lm.getBestProvider(crit, false);
    Location location = lm.getLastKnownLocation(towers);

    if (location != null){
        lat = (int) (location.getLatitude() *1E6);
        longi= (int) (location.getLongitude() *1E6);



        GeoPoint ourLocation = new GeoPoint(lat,longi);
        OverlayItem overlayItem = new OverlayItem(ourLocation, "Hi!!", "2nd");
        CustomPinPoint custom = new CustomPinPoint(d, Main.this);   
        custom.insertPinpoint(overlayItem);
        overlayList.add(custom);
    }else{
       Toast.makeText(Main.this, "Couldnt get provider", Toast.LENGTH_SHORT).show();
    }





}

@Override
protected void onPause() {
    // TODO Auto-generated method stub

    super.onPause();
    lm.removeUpdates(this);
}


     @Override
protected void onResume() {
    // TODO Auto-generated method stub

    super.onResume();
    lm.requestLocationUpdates(towers, 500, 1, this );
}


    @Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}



class Touchy extends Overlay{
    public boolean onTouchEvent(MotionEvent e, MapView m){
    if (e.getAction() == MotionEvent.ACTION_DOWN){
        start = e.getEventTime();

    }
    if (e.getAction() == MotionEvent.ACTION_UP){
        stop = e.getEventTime();
        x = (int) e.getX();
        y = (int) e.getY();
        touchedPoint = map.getProjection().fromPixels(x, y);

    }
    if (stop - start > 1500){
        AlertDialog alert = new AlertDialog.Builder(Main.this).create();
        alert.setTitle("Pick Option");


        alert.setButton("Hello", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

    OverlayItem overlayItem = new OverlayItem(touchedPoint, "Hi!!", "2nd");
        CustomPinPoint custom = new CustomPinPoint(d, Main.this);   
                custom.insertPinpoint(overlayItem);
                overlayList.add(custom);



        }
        });
    alert.setButton3("Get Address", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
    Geocoder geocoder = new Geocoder(getBaseContext(), Locale.getDefault());
    try{
    List<Address> address = geocoder.getFromLocation(touchedPoint.getLatitudeE6() /      1E6, touchedPoint.getLongitudeE6() / 1E6 , 1);
                if (address.size() > 0){
                    String display = "";
        for (int i = 0; i<address.get(0).getMaxAddressLineIndex(); i++){

                display += address.get(0).getAddressLine(i) + "\n";
                    }
    Toast t = Toast.makeText(getBaseContext(), display, Toast.LENGTH_LONG);
                    t.show();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
            }
        }});
    alert.setButton2("Toggle View", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

                if (map.isSatellite()){
                    map.setSatellite(false);
                    map.setStreetView(true);
                }else{
                    map.setStreetView(false);
                    map.setSatellite(true);
                }
            }
});


    alert.setButton("Place a Pin", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

    OverlayItem overlayItem = new OverlayItem(touchedPoint, "Hi!!", "2nd");
           CustomPinPoint custom = new CustomPinPoint(d,Main.this); 
                custom.insertPinpoint(overlayItem);
                overlayList.add(custom);

            }


        });
        alert.show();
        return true;
    }

        return false;
    }

}



public void onLocationChanged(Location l) {
    // TODO Auto-generated method stub
    lat = (int) (l.getLatitude() *1E6);
    longi = (int) (l.getLongitude() *1E6);
    GeoPoint ourLocation = new GeoPoint(lat,longi);
    OverlayItem overlayItem = new OverlayItem(ourLocation, "Hi!!", "2nd");
    CustomPinPoint custom = new CustomPinPoint(d, Main.this);   
    custom.insertPinpoint(overlayItem);
    overlayList.add(custom);

}




public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}




public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}




public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

    }

HelloWorld.java

HelloWorld.java

import com.google.android.maps.OverlayItem;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class HelloWorld extends Activity
{

        protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v)
            {
                Toast.makeText(HelloWorld.this, "you clicked on button![enter image description here][3]", Toast.LENGTH_LONG).show();
            }
});

} }

我也写

  <activity android:name=".HelloWorld" /> 

在我的清单

推荐答案

简单的方法

inlude 安卓的onClick =onClickMyButton按钮主要的属性的.xml

inlude android:onClick="onClickMyButton" as a attribute of Button in main.xml

删除 onClickListener 按钮

现在在你的主要活动定义这个马托

now define this mathod in your Main Activity

public void onClickMyButton(View view){

Intent intent = new Intent(Main.this, HelloWorld.class);
        startActivity(intent);


}

现在就开始你的应用程序应该正常工作

now start your application it should work fine