0
我輸入的文本這是我的輸入編輯文本類如何顯示來自其他類
package com.example.websocketclient;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText name;
Button enter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
name = (EditText) findViewById(R.id.editTextdname);
String dname = name.getText().toString();
sendname(dname);
}
});
}
public void sendname(String dname) {
Intent i = new Intent(this,Chatroom.class);
Bundle bundle = new Bundle();
bundle.putString("myname", dname);
i.putExtras(bundle);
startActivity(i);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
這是我顯示的EditText TextView的,以一流的
package com.example.websocketclient;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class Chatroom extends Activity {
TextView uname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chatroom);
uname = (TextView) findViewById(R.id.textViewmynam);
//kuha
Bundle bundle = getIntent().getExtras();
String urname = bundle.getString("myname");
uname.setText(urname);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.chatroom, menu);
return true;
}
}
當我運行它..它說不幸的是,那麼它會關閉.. 我想從 - > EditText傳遞數據到我的其他類到我的顯示類 - > TextView。
你只是忘了在'onCreate()'方法中'初始化'''輸入''Button'。 – Piyush 2014-10-17 05:42:47