Java 如何为android studio中onClick块内先前定义的字符串赋值
因此,我有两个活动通过意图相互链接,但我需要第二个页面来了解上一个页面中单击的内容。这就是我的意思Java 如何为android studio中onClick块内先前定义的字符串赋值,java,android,string,Java,Android,String,因此,我有两个活动通过意图相互链接,但我需要第二个页面来了解上一个页面中单击的内容。这就是我的意思 public class featuredvendors extends AppCompatActivity { public String nextpageref; 上面声明了我要编辑的字符串 t2.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){
public class featuredvendors extends AppCompatActivity {
public String nextpageref;
上面声明了我要编辑的字符串
t2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
nextpageref="post1";
Intent t2 = new Intent(featuredvendors.this, featureddetails.class);
startActivity(t2);
}
});
所以本质上,我想根据点击的内容更改nextpageref的值。但是,似乎我无法更改onClick块内的值。我是否可以将值“post1”传递给字符串,但仅当单击此特定按钮时?我需要这样做,因为下一页“featureddetails”需要知道这个字符串值,以便从数据库中提取正确的信息。在
onClick
块中添加这个
t2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
nextpageref="post1";
Intent t2 = new Intent(featuredvendors.this, featureddetails.class);
t2.putString("key", nextpageref);
startActivity(t2);
}
});
在下一个活动中,从onCreate方法中的这段代码中获取值
Intent i= getIntent();
String value = i.getStringExtra("key");
Log.e("value", value);
发送活动
Intent intent = new Intent(SendingActivity.this, RecievingActivity.class);
intent. putString("keyName", value); // pass your values and retrieve them in the other Activity using keyName
startActivity(intent);
Second Screen.java
公共类newclass扩展活动
{
私有文本视图文本电视
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intent);
Textv = (TextView)findViewById(R.id.tv2);
Intent i= getIntent();
i.getStringExtra("keyName");
}
}类名的可能重复最好以每个单词的大写字母开头:FeaturedVendors、FeaturedDetails等