所以假设我有4个按钮,每个按钮包含导航到某个活动的意图。他们都导航到一个相同的活动。当我点击第一个按钮时,我希望新活动显示“嗨”,当我点击第二个按钮时,我希望它显示“再见”等等。我怎么做 ? 这是一个以
开头的简单代码public class Intentt extends Activity {
Button b1,b2,b3,b4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intentt);
b1 = (Button)findViewById(R.id.button2);
b2 = (Button)findViewById(R.id.button3);
b3 = (Button)findViewById(R.id.button4);
b4 = (Button)findViewById(R.id.button5);
b1.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
startActivity(i);
}
});
b2.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
startActivity(i);
}
});
b3.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
startActivity(i);
}
});
b4.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
startActivity(i);
}
});
}
答案 0 :(得分:3)
只是通过意图附加内容传递一些数据,或者意图额外Bundle
:
Intent i=new Intent(context,MainActivity.class);
i.putExtra(id, 453);
context.startActivity(i);
示例:
Button b1, b2, b3, b4;
Context context = this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intentt);
b1 = (Button) findViewById(R.id.button2);
b2 = (Button) findViewById(R.id.button3);
b3 = (Button) findViewById(R.id.button4);
b4 = (Button) findViewById(R.id.button5);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(context, MainActivity.class);
i.putExtra(MainActivity.ID_ACTION, MainActivity.ACTION_1);
startActivity(i);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(context, MainActivity.class);
i.putExtra(MainActivity.ID_ACTION, MainActivity.ACTION_2);
startActivity(i);
}
});
}
然后在那个活动中你可以打开这些值,就像那样:
String id = intent.getStringExtra("id");
int id = intent.getIntExtra("id",-1);
然后使用if-elseif-else
或switch
语句根据传递的操作更改操作,以便显示消息
要正确接收它们,您需要在AndroidManifest.xml
中将活动的launchMode设置为“标准”because of this bug并创建
public static final int ACTION_1 = 1;
public static final int ACTION_2 = 2;
public static final int ACTION_NULL = -1;
public static final String ID_ACTION = "action_id";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int id = getIntent().getIntExtra(ID_ACTION, -1);
if (id == ACTION_NULL) {
Log.d("TAG", "id is null");
Toast.makeText(MainActivity.this, "id is null!", Toast.LENGTH_SHORT).show();
} else if (id == ACTION_1) {
Log.i("TAG", "ALLOHA! from button 1");
Toast.makeText(MainActivity.this, "Aloha from button 1!", Toast.LENGTH_LONG).show();
} else if (id == ACTION_2) {
Log.i("TAG", "Hello from button 2");
Toast.makeText(MainActivity.thi,"Hello from button 2!", Toast.LENGTH_LONG).show();
}
}
答案 1 :(得分:1)
在每个意图中,您都可以传递参数
Intent i=new Intent(context,SendMessage.class);
i.putExtra("KEY_MESSAGE", "Hola amigo");
在其他活动中
Intent i = getIntent();
String message = i.getStringExtra("KEY_MESSAGE")
Toast.make(this,message,Toast.LENGTH_SHORT).show()
答案 2 :(得分:1)
重新编写按钮单击侦听器,如下所示:
b1.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
i.putExtra("Data","Hi");
startActivity(i);
}
});
b2.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
i.putExtra("Data","Hello");
startActivity(i);
}
});
b3.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
i.putExtra("Data","Bye");
startActivity(i);
}
});
b4.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
i.putExtra("Data","See you");
startActivity(i);
}
});
在您的MainActivity类onCreate方法中,您可以通过以下方式访问传递的数据:
String passedData = getIntent().getStringExtra("Data");
您可以使用此传递的数据在屏幕上显示。
答案 3 :(得分:1)
这样做:
class Program
{
static Socket socketForClient;
static NetworkStream networkStream;
static System.IO.StreamReader streamReader;
static System.IO.StreamWriter streamWriter;
static TcpListener tcpListener;
static int PORT;
static void Main(string[] args)
{
//Console.WriteLine("Enter Port : ");
//PORT = Convert.ToInt32(Console.ReadLine());
PORT = 12500;
Console.WriteLine("\n" + "Your Host Information Is : "+"\n" );
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress ip in localIPs)
{
Console.WriteLine(ip.ToString());
}
while (true)
{
tcpListener = new TcpListener(PORT);
Console.WriteLine("\n >> Server started ... Waiting for Message");
tcpListener.Start();
try
{
socketForClient = tcpListener.AcceptSocket();
}
catch (System.Exception e1)
{
Console.WriteLine("LN: 40");
Console.WriteLine("Message: " + e1.Message);
Console.WriteLine("InnerException: " + e1.InnerException);
Console.WriteLine("Source: " + e1.Source);
Console.ReadLine();
}
if (socketForClient.Connected)
{
Console.WriteLine("Client connected");
try
{
networkStream = new NetworkStream(socketForClient);
}
catch (System.Exception e1)
{
Console.WriteLine("LN: 55");
Console.WriteLine("Message: " + e1.Message);
Console.WriteLine("InnerException: " + e1.InnerException);
Console.WriteLine("Source: " + e1.Source);
Console.ReadLine();
}
//System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream);
try
{
streamReader = new System.IO.StreamReader(networkStream);
streamWriter = new System.IO.StreamWriter(networkStream);
}
catch (System.Exception e1)
{
Console.WriteLine("LN: 71");
Console.WriteLine("Message: " + e1.Message);
Console.WriteLine("InnerException: " + e1.InnerException);
Console.WriteLine("Source: " + e1.Source);
Console.ReadLine();
}
string theString = "Sending";
// streamWriter.WriteLine(theString);
Console.WriteLine(theString);
//streamWriter.Flush();
long x = 0;
try
{
x++;
theString = streamReader.ReadLine();
if (theString.Trim() == "4-")
{
Console.WriteLine("Sending Report Data : 0-100-0-0");
streamWriter.WriteLine("0-70-0-0");
Console.WriteLine("pocket sent");
streamWriter.Flush();
}
Console.WriteLine(theString + x.ToString());
streamReader.Close();
networkStream.Close();
socketForClient.Close();
tcpListener.Stop();
}
catch (System.Exception e1)
{
streamReader.Close();
networkStream.Close();
socketForClient.Close();
Console.WriteLine("LN: 97");
Console.WriteLine("Message: " + e1.Message);
Console.WriteLine("InnerException: " + e1.InnerException);
Console.WriteLine("Source: " + e1.Source);
Console.ReadLine();
break;
//streamWriter.Close();
}
}
}
streamReader.Close();
networkStream.Close();
socketForClient.Close();
Console.WriteLine("Closed Socket");
Console.ReadLine();
}
}
之后在您发送的mainActivity检索字符串中:
public class Intentt extends Activity implements View.OnClickListener {
Button b1,b2,b3,b4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intentt);
b1 = (Button)findViewById(R.id.button2);
b2 = (Button)findViewById(R.id.button3);
b3 = (Button)findViewById(R.id.button4);
b4 = (Button)findViewById(R.id.button5);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button2:
Intent i = new Intent(Intentt.this,MainActivity.class);
i.putExtra('"STRING_I_NEED"', "Hi");
startActivity(i)
break;
case R.id.button3:
Intent i = new Intent(Intentt.this,MainActivity.class);
i.putExtra('"STRING_I_NEED"', "Bye");
startActivity(i)
break;
case R.id.button4:
break;
.
.
.
}
}
}