【问题标题】:button settext to next class按钮 settext 到下一堂课
【发布时间】:2013-08-07 11:12:11
【问题描述】:

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

first.class

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


}
});  

【问题讨论】:

  • 但是,那里会有什么文字?意味着您会从您的第一个活动中动态获取它还是默认文本存在?
  • 默认文本有我如何编码?
  • 嘿,我已经更新了我的答案

标签: android button settext


【解决方案1】:
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】:

    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>
    

    【讨论】:

    • 为什么要恢复?新手问题
    • 我放了它是因为每次您的活动开始时,默认情况下都会出现该消息。如果您想动态设置它,那么您必须从用户那里获取它,在这种情况下,情况并非如此。
    • 如果我想要 tv.setText("Hello");来自头等舱的按钮?这可能吗?每次我点击第一堂课的按钮时,即使我打算在其他课程中使用它,也会有你好
    • 还有一件事我如何在不实现 textview 的情况下实现它。我的意思是它会自动为 settext 制作一个 textview
    • 是的,我是新人。呵呵。我真的需要为我的 xml 制作一个 textview 以便文本显示给我吗?如果我单击按钮,是否有任何代码可以生成文本视图和文本?
    【解决方案3】:

    这样做:

    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 >
    

    【讨论】:

    • 在布局文件夹中制作 second.xml
    • 我的意思是。如果我单击 first.class 中的按钮,则会在 second.class 中显示一个 settext
    【解决方案4】:

    你的问题有点不清楚。

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

    如果不是你想要的,请告诉我,我会看看我能想到什么,但请尝试解释得更好

    祝你好运!

    【讨论】:

    • 是的,你明白我的意思,非常感谢你的回答。但我在 intent.putExtra("text_identifier, "The text to show"); startActivity(intent); 中有错误
    • 您遇到什么错误?这段代码是从我的工作代码中复制和粘贴的(我注意到您发布的行中缺少结束引号)
    • 另外,如果您发现此答案(或任何其他)有帮助,请投票(问题左侧的向上按钮),如果您认为它对您的问题正确,请接受(在同一个地方打勾)Welcome to SO
    猜你喜欢
    • 2021-01-08
    • 2014-12-05
    • 1970-01-01
    • 2014-09-22
    • 2010-12-31
    • 1970-01-01
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多