java - 使用 Android 应用程序调用 Textview 值

标签 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>

字符串.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 个旅行名称及其联系电话。 我想通过应用程序打电话。因此,如果我点击特定的旅行,它应该重定向到该号码的调用。 我哪里出错了?请帮忙。提前致谢。

最佳答案

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
    }  

关于java - 使用 Android 应用程序调用 Textview 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30310499/

相关文章:

android - 如何在 Firebase 中聆听多个 child 的声音?

java - 我如何在 Java 中获取 XML 元素的兄弟元素(使用 DOM 解析器)?

java - 缩小操作栏后,抽屉导航切换不起作用

java - 将 .HBM 模型转换为带注释的 pojo

Java XmlRPC 身份验证

java - 如何将 JUnit4 的测试通过/失败结果记录到文件中?

android - 使用android openCV进行实时视频处理

java - 当实例可能具有不同的数据类型时,如何返回该实例的正确数据类型?

android - 在代码中旋转一个按钮(或里面的文字)

java - sendMessage() 方法在哪里找?