按钮settext到下一课

时间:2013-08-07 11:12:11

标签: android button settext

我有两节课; first.classsecond.class。在我的first.class上有一个按钮,在我的second.class上没有按钮。我需要它,所以当点击first.class中的按钮时,会有一个文本显示在second.class上,并带有保存选项。

first.class

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


}
});  

4 个答案:

答案 0 :(得分: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>

second.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 >

答案 1 :(得分: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;
}
}

答案 2 :(得分:0)

你的问题有点不清楚。

当您说first.classsecond.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
}

如果不是你想要的,请告诉我,我会看到我能想到的,但请尝试解释一下

祝你好运!

答案 3 :(得分: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);
            }
        });
    }


}

second.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>

Second.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>