将一个Activity共享成一个Action供其他程序调用

假设我们现在写好了一个字典的程序,在输入框输入字母,就能显示他的英语翻译,现在我们将这个程序共享成一个Action,这样其他程序就可以通过调用这个Action来访问这个程序。

字典程序的处理方法如下:


                if (getIntent().getData() != null)
		{
			
			String word = getIntent().getData().getHost();
			String sql = "select chinese from t_words where english=?";
			database = openDatabase();
			Cursor cursor = database.rawQuery(sql, new String[]
			{ word });
			String result = "未找到该单词.";
			if (cursor.getCount() > 0)
			{
				cursor.moveToFirst();
				result = cursor.getString(cursor.getColumnIndex("chinese"));
			}
			textview.setText(result);
		}

为了能够使用这个Action,我们需要在mainfirst中配置:

		<activity android:name=".Main" android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
		<activity android:name=".TranslateWord" android:theme="@android:style/Theme.Dialog">
			<intent-filter>
				<action android:name="net.DICTIONARY" />
				<data android:scheme="dict" />
				<category android:name="android.intent.category.DEFAULT" />
			</intent-filter>
		</activity>
这样通过访问定义的Schema的值,就可以调用这个Action了

在另一个程序中,我们这样调用这个Action:

	public void onClick(View view)
	{
		Intent intent = new Intent("net.DICTIONARY", Uri
				.parse("dict://" + etWord.getText().toString()));

		startActivity(intent);

	}



要将一个Activity转换一个View并显示,可以按以下步骤操作: 1. 在Activity中,创建一个布局文件,用来显示界面上的各个控件和视图。 2. 在Activity中重写onCreateView()方法,在该方法中加载布局文件,生View对象并返回。 3. 在Activity中获取FragmentManager对象,并通过调用FragmentManager.beginTransaction()方法创建一个FragmentTransaction对象。 4. 在FragmentTransaction对象中,使用add()方法将生的View对象添加到要显示它的布局容器中。 5. 调用commit()方法提交事务,完Activity转换View并显示的操作。 以下是一个简单的示例代码: ``` public class MyActivity extends Activity { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // 加载布局文件 View view = inflater.inflate(R.layout.my_layout, container, false); // 返回生的View对象 return view; } public void showAsView(ViewGroup container) { // 获取FragmentManager对象 FragmentManager fragmentManager = getFragmentManager(); // 创建FragmentTransaction对象 FragmentTransaction transaction = fragmentManager.beginTransaction(); // 将生的View对象添加到要显示它的布局容器中 transaction.add(container.getId(), this); // 提交事务,完Activity转换View并显示的操作 transaction.commit(); } } ``` 在代码中,MyActivity继承自Activity类,并重写了onCreateView()方法,用来加载布局文件并生View对象。同时,还提一个showAsView()方法,用来将该Activity转换View并显示在指定的布局容器中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值