Javascript 将按钮上的文本输出到下一个活动
在第一个活动中,我有一个带有多个按钮的应用程序。已将“完成”按钮配置为在单击时打开第二个活动。我希望在单击时能够查看第二个活动中其他按钮上的文本。我之前在第一个活动中的所有按钮上设置了onclick Listener,然后在第二个活动中调用了onclick协议,如下所示:Javascript 将按钮上的文本输出到下一个活动,javascript,android,jquery,button,android-activity,Javascript,Android,Jquery,Button,Android Activity,在第一个活动中,我有一个带有多个按钮的应用程序。已将“完成”按钮配置为在单击时打开第二个活动。我希望在单击时能够查看第二个活动中其他按钮上的文本。我之前在第一个活动中的所有按钮上设置了onclick Listener,然后在第二个活动中调用了onclick协议,如下所示: public void onClick(View v) { ((Button) v).getText(); } 但它不起作用。我只是想知道在第二个活动中调用onClick协议是否是正确的,如果不是,如何进行。我还尝试
public void onClick(View v) {
((Button) v).getText();
}
但它不起作用。我只是想知道在第二个活动中调用onClick协议是否是正确的,如果不是,如何进行。我还尝试了以下代码:
public void onClick(View v) {
// 1) Possibly check for instance of first
Button b = (Button)v;
String buttonText = b.getText().toString();
}
谢谢你的帮助
以下是所有按钮都存在的第一个活动的代码:
package com.example.coursework1;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Button;
import android.view.View;
public class MainActivity extends Activity implements View.OnClickListener {
Button button7;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button7 = (Button)findViewById(R.id.button7);
button7.setOnClickListener(this);
//
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
}
@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;
}
private void FinishClick()
{
startActivity(new Intent("com.example.coursework1"));
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button7:
FinishClick();
break;
}
// TODO Auto-generated method stub
}
}
这是它的.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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/button1"
android:layout_marginRight="68dp"
android:text="@string/Panini" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="41dp"
android:layout_toRightOf="@+id/textView1"
android:text="@string/Tea" />
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginTop="49dp"
android:layout_toRightOf="@+id/textView1"
android:text="@string/Sandwich" />
<Button
android:id="@+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button3"
android:layout_alignBottom="@+id/button3"
android:layout_alignLeft="@+id/button2"
android:text="@string/Salad" />
<Button
android:id="@+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/button3"
android:layout_marginTop="48dp"
android:text="@string/Water" />
<Button
android:id="@+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button4"
android:layout_alignTop="@+id/button5"
android:text="@string/Juice" />
<Button
android:id="@+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dp"
android:text="@string/Finish" />
</RelativeLayout>
此时,按钮7在单击时打开第二个活动。我还想查看在第二个活动中单击的任何其他按钮的文本。您可以通过intent本身传递此类简单字符串
Intent newintent=new Intent(getApplicationContext(), NextActivity.class);
newintent.putExtra("type", 1);
startActivity(resturatntintent);
论触觉
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
type=getIntent().getExtras().getInt("type", 0);
....
}
编辑:
在您的
main活动中
在MainActivity中全局声明哈希集
private HashSet<String> hashSet;
此方法是View.OnClickListener
onClick
方法的实现
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
hashSet.add("BUTTON1");
break;
case R.id.button2:
hashSet.add("BUTTON2");
break;
case R.id.button3:
hashSet.add("BUTTON3");
break;
// Do the same for remaining buttons
case R.id.button7:
Intent intent = new Intent("com.example.coursework1");
intent.putExtra("BUTTONS_DATA", hashSet);
startActivity(intent);
break;
}
在您的FinishButton
中,检索HashSet
,您可以通过字符串获取单击哪些按钮的数据
Bundle extras = getIntent().getExtras();
if (extras != null) {
HashSet<String> hashSet = (HashSet<String>) extras
.get("BUTTONS_DATA");
System.out.println("These buttons are clicked in the FirstActvity");
for (String string : hashSet) {
System.out.println(string);
}
}
Bundle extras=getIntent().getExtras();
如果(附加值!=null){
HashSet HashSet=(HashSet)附加值
.获取(“按钮和数据”);
System.out.println(“这些按钮在第一个活动中被单击”);
for(字符串:hashSet){
System.out.println(字符串);
}
}
我编写了Android,它是一个小型框架,提供了对常见API的抽象,使类似这样的任务变得更容易。因此,在您的情况下,构建您的意图:
$.Intent(this, FinishButton.class).put("buttonText", buttonText);
// NOTE: 'this' is a context handle, so your current activity in this case
然后在FinishButton
活动中:
protected void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.finish_button);
String buttonText = $.Intent(getIntent()).extras().getString("buttonText");
}
同样在VaporBundle
和VaporIntent
中,.put()
方法被重载,因此您可以对其抛出任何支持的类型:)
如果您愿意,可以试一试。所有内容都是免费的。您的问题可能看起来很清楚,如果您提供有关重新编码的更多信息,您好,对不起,这是不是说Restaurantint?Restaurantint是否指代了我的第一项活动?这是否也使用了按钮上的onclick监听器,还是不需要呢?抱歉,没有重申它的新意图。只需通过intent extras传递特定按钮的字符串,只需检查如何将字符串作为extra添加到intent中,并从中检索相同的字符串。您好,我在MainActivity中的hashSet上收到一个错误。我还收到一个错误,说“MainActivity类型的onClick1(视图)方法必须重写或实现一个超类型方法”。我重新命名了onClick,因为按钮7上有重复的onClick。嗨,对不起,你能解释清楚我将如何使用它吗?我对java和android非常陌生。我链接的网站上有很多信息,但基本上它提供了类似于jQuery的API,可以是简洁而有用的抽象层。我在上面使用的代码首先展示了如何创建意图(用于启动新活动),然后展示了如何在新启动的FinishButton活动实例上调用的onCreate方法中检索此原始数据
Bundle extras = getIntent().getExtras();
if (extras != null) {
HashSet<String> hashSet = (HashSet<String>) extras
.get("BUTTONS_DATA");
System.out.println("These buttons are clicked in the FirstActvity");
for (String string : hashSet) {
System.out.println(string);
}
}
$.Intent(this, FinishButton.class).put("buttonText", buttonText);
// NOTE: 'this' is a context handle, so your current activity in this case
protected void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.finish_button);
String buttonText = $.Intent(getIntent()).extras().getString("buttonText");
}