使用Android应用调用Textview值

时间:2015-05-18 18:34:00

标签: java android xml android-layout

我正在做一个Android应用,我想要显示旅行联系号码。这是代码:

App2Activity.java

package com.example.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class App2Activity extends Activity {

    Button button;

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

AppActivity.java

package com.example.android;

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

public class AppActivity extends Activity {
    Button button;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        addListenerOnButton();
    }


public void addListenerOnButton() {
    final Context context = this;
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(context, App2Activity.class);
            Intent intent = new Intent(Intent.ACTION_CALL);
                        startActivity(intent);   
        }
    });
}

}

main.xml中

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Travels and Holidays" />

</LinearLayout>

main2.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/travels"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

的strings.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="app_name">Travels and Holidays Details</string>

    <string name="travels">\n
    Nirmala travels - 0824-2497051 \n 

    RR Tours and Travels - 0824-4280999 \n

    Surabhii Travels - 0824-2212111 \n

    </string>

</resources>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.CALL_PHONE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".AppActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:label="@string/app_name"
            android:name=".App2Activity" >
        </activity>
    </application>

</manifest>

点击“旅行和假期”按钮后,会显示3个旅行姓名及其联系电话。 我想打电话通过应用程序。因此,如果我点击特定旅行,它应该重定向到该号码的呼叫。 我哪里错了?请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:1)

AppActivity.java

public class AppActivity extends Activity {
Button button;
Context context = this;

private final CharSequence[] TRAVELS ="Nirmala travels","RR Tours and Travels","Surabhii Travels"};
String numbertodial;
String phonenumberNT ="08242497051";
String phonenumberRR ="08244280999";
String phonenumberST ="08242212111";

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

button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    Dialog dialog = null;
    AlertDialog.Builder builder = null;
    builder = new AlertDialog.Builder(context);

    String travels = getString(R.string.app_name);
    builder.setTitle(travels);
    builder.setSingleChoiceItems(TRAVELS, 3,
    new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {

    CharSequence s = (TRAVELS[item]);


    //THIS CAN BE IMPROVED. I DONT HAVE THE TIME BUT IT SHOULD STILL WORK. 
    if (s.equals("Nirmala travels")){numbertodial =phonenumberNT; }
    if (s.equals("RR Tours and Travels")){numbertodial=phonenumberRR; }
    if (s.equals("Surabhii Travels")){numbertodial=phonenumberST ;}
    Intent callIntent = new Intent(Intent.ACTION_DIAL);
    callIntent.setData(Uri.parse("tel:"+numbertodial ));
    startActivity(callIntent);

    dialog.dismiss();
    }});
    dialog = builder.create();
    dialog.show();
    return;

        }
    });
//REMOVE addListenerOnButton(); and it's method
    }