gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:32:07 24 4
gpt4 key购买 nike

我正在开发一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com