有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

将java文本视图转换为短信正文Android

我正在为Android开发一个应用程序,当你打开应用程序时,它会通过GPS在文本视图中显示你的精确坐标。然后你按下一个按钮,一条短信就会被发送到一个预设的号码和你的坐标。我所有的东西都在工作,除了短信的身体。我不知道如何把你的坐标和文本视图发送到短信正文中。任何帮助都会很棒

JAVA主页

package com.example.test;

import 安卓.app.Activity;
import 安卓.content.Context;
import 安卓.location.Location;
import 安卓.location.LocationListener;
import 安卓.location.LocationManager;
import 安卓.os.Bundle;
import 安卓.telephony.gsm.SmsManager;
import 安卓.util.Log;
import 安卓.view.View;
import 安卓.view.View.OnClickListener;
import 安卓.widget.Button;
import 安卓.widget.TextView;
import 安卓.widget.Toast;

@SuppressWarnings("deprecation")
public class MainActivity extends Activity implements LocationListener{
    protected LocationManager locationManager;
    protected LocationListener locationListener;
    protected Context context;
    TextView txtLat;
    String lat;
    String provider;
    protected String latitude,longitude; 
    protected boolean gps_enabled,network_enabled;
    Button button;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtLat = (TextView) findViewById(R.id.textview1);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1000, this);




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

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

            button.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    try {

                        String messageToSend = "R.id.textview1";
                        String number = "8143664050";

                        SmsManager.getDefault().sendTextMessage(number, null, messageToSend, null,null);


                    } catch (Exception e) {
                        Toast.makeText(getApplicationContext(), "SMS failed!",
                                Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }
                }
            });
        }

    }

    @Override
    public void onLocationChanged(Location location) {
        txtLat = (TextView) findViewById(R.id.textview1);
        txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.d("Latitude","disable");
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.d("Latitude","enable");

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.d("Latitude","status");
    }

}

XML文件

<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        安卓:id="@+id/textview1"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_centerHorizontal="true"
        安卓:layout_centerVertical="true"
        安卓:text="@string/hello_world" />

    <Button
        安卓:id="@+id/button1"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_alignParentTop="true"
        安卓:layout_alignRight="@+id/textview1"
        安卓:layout_marginRight="16dp"
        安卓:layout_marginTop="22dp"
        安卓:onClick="gotoActivity"
        安卓:text="Button" />

</RelativeLayout>

共 (1) 个答案

  1. # 1 楼答案

    尝试改变这一点:

    String messageToSend = "R.id.textview1";
    

    对这个

    TextView tv = (TextView) findViewById(R.id.textview1);
    String messageToSend = tv.getText().toString();