简体   繁体   English

Android中的Java从一个屏幕切换到另一个屏幕

[英]Java in Android from one screen to another screen

I am doing a Android app where I want to show the list of travels contact numbers in a separate screen when clicked on "Travels and Holidays" button. 我正在做一个Android应用程序,当我单击“旅行和假期”按钮时,我想在一个单独的屏幕中显示旅行联系电话列表。 Here is the code: 这是代码:

AppActivity.java 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;
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]);

    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;

        }
    });

    }  
}

main.xml 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>

strings.xml strings.xml中

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

<resources>

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

</resources>

AndroidManifest.xml 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>

    </application>

</manifest>

When clicked on "Travels and Holidays" button, it shows the 3 travels names in the same screen. 单击“旅行和假期”按钮时,它将在同一屏幕上显示3个旅行名称。 When selecting any 1, it directly goes to call that number. 选择任意1时,它将直接拨打该号码。

But what I want is: When I click on "Travels and Holidays" button in the first screen, I want the travels name and contact number to be shown on another screen. 但是我想要的是:当我在第一个屏幕上单击“旅行和假期”按钮时,我希望旅行名称和联系电话显示在另一个屏幕上。 When clicked on any travels, it should redirect to the call option of that number. 单击任何旅行时,它都应重定向到该号码的呼叫选项。

Where am I going wrong? 我要去哪里错了? Please help. 请帮忙。 Thanks in advance 提前致谢

You have to create Another Activity and then You redirect to that place and put yourno and name in that. 您必须创建另一个活动,然后将您重定向到该位置,然后在其中输入您的编号和名称。

 Intent i=new Intent(yourcontext,yourActivity.class);
    i.putExtra("no","yourno");
    i.putExtra("name","yourname");
    startActivity(i);

and get to youranothetactivity Using 并使用

String no=getIntent().getStringExtra("no");
String name=getIntent().getStringExtra("no");

for more info You see this link 有关更多信息,您会看到此链接

Create a separate activity/fragment to show " Travels and Holidays" details. 创建一个单独的活动/片段以显示“ 旅行和假期”详细信息。 Call this activity/fragment on " Travels and Holidays " button click, Pass " Travels and Holidays " details value to the Details activity/fragment using Intent putExtra(). 在“ 旅行和假期 ”按钮单击时调用此活动/片段,使用Intent putExtra()将“ 旅行和假期 ”详细信息值传递给Details活动/片段。 If you create Activity then make sure to declare activity inside AndroidManifest.xml 如果创建Activity,请确保在AndroidManifest.xml中声明活动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM