Android-如何进行文本显示

时间:2010-08-27 05:41:23

标签: android

我是新手。我有一些代码......我有

Simple.java:

 public class Simple extends Activity {
     /** Called when the activity is first created. */
      Button show;
      TextView view;
    EditText edit;

 @Override
  public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       show=(Button)findViewById(R.id.show);
      view=(TextView)findViewById(R.id.view);
      edit=(EditText)findViewById(R.id.edit);


show.setOnClickListener(new OnClickListener(){
    public void onClick(View view){

    show();

    }
});

}

public void show(){
    String text=edit.getText().toString();
    view.setText(text);
    Intent t=new Intent(this,Show.class);
    startActivity(t);

}

}

当我尝试在相同的活动中显示文本时,它有效... 我试图传递我在EditText中键入的文本并将其显示给Show.class

Show.class的代码

public class Show extends activity {

private Simple simple;
TextView text1;
Button back;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.next);
    back=(Button)findViewById(R.id.button);
    text1=(TextView)findViewById(R.id.then);

    back.setOnClickListener(new OnClickListener(){
        public void onClick(View view){
            start();
        }
    });
}
public void start(){
        String t=simple.edit.getText().toString();
        text1.setText(t);

    Intent t=new Intent(this,Simple.class);
    startActivity(t);
}

}

我试过这个我无法在Simple.java中显示我在editText中给出的文本。 我知道这是基本的,但我知道。所以请帮帮我。谢谢你...

2 个答案:

答案 0 :(得分:1)

尝试将此添加到show():

Intent t = new Intent(this, Simple.class);
t.putExtra("editText", text);
startActivity(t);

然后在Show class的start()方法中,使用:

Intent t = getIntent();
Bundle data = t.getExtras();
text1.setText(data.getString("editText"));

我还没有对此进行过测试(并且您的实现稍有混淆......)但是putExtra和getExtra函数是您可能希望使用的函数。

答案 1 :(得分:0)

public class Intents extends Activity {
    /** Called when the activity is first created. */

    EditText edit;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button = (Button) findViewById(R.id.start);
        button.setOnClickListener(mStartListener);
        Button button1 = (Button) findViewById(R.id.start1);
        button1.setOnClickListener(activity2);
        edit=(EditText)findViewById(R.id.edit);
        Button show=(Button)findViewById(R.id.show);
        show.setOnClickListener(activity3);

    }
    private OnClickListener mStartListener = new OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(Intents.this, 
                    startactivity1.class));

        }
    };
    private OnClickListener activity2 = new OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(Intents.this, 
                    startactivity2.class));        
        }
    };

    private OnClickListener activity3 = new OnClickListener() {
        public void onClick(View v) {
            String text=edit.getText().toString();
            Intent t = new Intent(Intents.this, startactivity3.class);
            t.putExtra("editText", text);
            startActivity(t);

            //startActivity(new Intent(Intents.this, 
                  //  startactivity3.class));        
        }
    };
}





public class startactivity3 extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actv3);
     //   Button ok = (Button)findViewById(R.id.but);
        TextView text1=(TextView)findViewById(R.id.vi);
        Intent t = getIntent();
        Bundle data = t.getExtras();
        text1.setText(data.getString("editText"));

    }   

}

不要忘记在Android清单文件中添加新活动。转到您当前的应用程序文件单击应用程序并在我的情况下添加您的新活动我将添加showactivity3在您的情况下您必须在您的清单中显示活动。试试这个经过测试的代码