1

我有两节课;first.classsecond.class。_ 我的first.class有一个按钮,我second.class的没有按钮。我需要它,所以当单击按钮时first.class,将出现一个文本,并second.class带有保存它的选项。

头等舱

button.setOnClickListener(new View.OnClickListener() {
    @Override   
   public void onClick(View v) {


}
});  
4

4 回答 4

0

做这个:

button.setOnClickListener(new View.OnClickListener() {
    @Override   
   public void onClick(View v) {

    Intent p = new Intent(first.this,
                         second.class);
     startActivity(p);


}

});

然后在第二个类中加载一个包含 textview 的 xml 文件

package ......;

import java.io.File;
import java.io.IOException;

import dolphin.devlopers.com.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class gmail1 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second); //the file which contains textview and ui

        TextView  tv = (TextView)findViewById(R.id.textView1);
  }

清单文件:

<Activity 
  android:name="first">
</Activity>

<Activity 
  android:name="second">
</Activity>

第二个.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hi"
       />

</RelativeLayout >
于 2013-08-07T11:19:49.287 回答
0

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="57dp"
        android:layout_marginTop="23dp"
        android:text="Click" />

</RelativeLayout>

MainActivity.java

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

public class MainActivity extends Activity {

    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent in = new Intent(getBaseContext(), Second.class);
                startActivity(in);
            }
        });
    }


}

第二个.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

</LinearLayout>

二.java

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

public class Second extends Activity {

    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        tv = (TextView)findViewById(R.id.textView1);
}


    @Override
      public void onResume()
      {
          super.onResume();
          tv.setText("Hello");

      }
}

显现

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.f.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Second" />
    </application>

</manifest>
于 2013-08-07T11:29:50.350 回答
0
public class first{
button.setOnClickListener(new View.OnClickListener() {
    @Override   
   public void onClick(View v) {

    second.setText(text);
}
});
}

public class second{
private static String textFromFirst;

public static void setText(String text){
second.textFromFirst = text;
}
}
于 2013-08-07T11:24:45.243 回答
0

你的问题有点不清楚。

当您说first.classandsecond.class时,您的意思是它们都扩展了Activity吗?意思是,它们是您应用程序中的两个不同“屏幕”,单击first活动上的按钮将启动second并更改其文本?如果是这样,您可以使用意图在活动之间传递信息。像这样的东西:

button.setOnClickListener(new View.OnClickListener() {
    @Override   
    public void onClick(View v) {
        Intent intent = new Intent(first.this, second.class);

        // NOW COMES THE IMPORTANT PART, Put the text you want to be passed
        intent.putExtra("text_identifier", "The text to show"); 

        startActivity(intent);
    }

});

在 Second.java 中:

private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
    tv = (TextView) findViewById(R.id.textView);

    // Retreive the text you sent eralier
    String theText = getIntent().getStringExtra("text_identifier");
    tv.setText(theText);
    // Do any extra onCreate things
}

如果这不是您所追求的,请告诉我,我会看看我能想到什么,但请尝试更好地解释一下

祝你好运!

于 2013-08-07T11:25:30.620 回答