1

我正在开发一个字典应用在Android 3+如何复制的文字传递给其他活动的Android

运行在活性1,存在其中用户输入的话秒的EditText上盒/他想抬头。然后使用Webview将单词的含义显示在Activity2中。

我知道在Android 3+中,用户可以长按Web视图上的项目并将其复制到剪贴板。因此,我正在考虑在Activity2中添加一个按钮来处理复制到剪贴板的任何文本。为了澄清,我希望当点击这个按钮时,Activity1将被调用并且复制的文本将被自动粘贴到它的EditText框中(用于查找)

我该如何以编程方式执行此操作?

如果您能提供示例和/或教程,我将不胜感激。非常感谢你提前。

+0

非常感谢您的帮助,伙计们。但对于我的无知感到抱歉,没有人提到如何将Activity2中复制的单词传送到剪贴板中,并且必须发送给Activity1的EditText。有任何想法吗? –

+0

您复制到剪贴板上的单词将存储在“String”中,对吗?只要您拥有特定的“字符串”,您就无需访问剪贴板内容。 –

回答

0

您可以使用共享首选项来存储字符串或其他值。 在共享偏好得到字符串,然后将其设置到编辑文本按钮单击事件使用的另一项活动..

0

在活动1:

SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()); 
Editor prefsEditor = appSharedPrefs.edit(); 
prefsEditor.putString("word1", string1); 
//so on for other 'n' number of words you have 
prefsEditor.commit(); 

在活动2:

SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()); 
String meaning1 = appSharedPrefs.getString("word1", "meaning not found"); 
//so on for other 'n' number of words 
1

使用intent将您的值从activity1传递到activity2

Intent i = new Intent(Activity1.this,Activity2.class); 
i.putExtra("MyValue", value); 
startActivityForResult(i, ActDocument.DIALOG_DOCUMENTDETAIL); 

In活性2

@Override 
    public void onCreate(Bundle savedInstanceState) { 
    //... 
    Intent intent = this.getIntent(); 
    value = intent.getSerializableExtra("MyValue"); 
    //... 
} 
0

在活动2:在点击按钮:

Intent it = new Intent(Activity2.this, Activity1.class); 
Bundle bundle=new Bundle(); 
bundle.putString("word", "Android"); 
it.putExtras(bundle); 
startActivity(it); 

在活动1:

Bundle bundle=getIntent().getExtras(); 
if(bundle !=null) 
{ 
String name=bundle.getString("word"); 
EditText edttxt=(EditText)findViewById(R.id.edtboxtest); 
edttxt.setText(name); 
} 
+0

非常感谢。但是Eclipse会抛出这个错误:'05-12 15:09:55.064:E/AndroidRuntime(1437):java.lang.RuntimeException:无法启动活动ComponentInfo {niamh.nadict/niamh.nadict.Nadict}:java.lang。 NullPointerException'。我想它有调用Activity1的问题。我把Activity1的东西放在OnCreate中。这样对吗? –

相关问题