android appwidget 笔记

AppWidget 就是我们使用的窗口小部件

实现appwidget非常简单,只需要一下几个步骤就OK

1.在 /res/layout 中建立一个 布局文件.此布局文件就是 窗体小部件 的"样子"

2.在/res/xml 中建立一个xml文件此文件对appwidget进行配置.如下

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:minWidth="1dp"	
    android:minHeight="1dp"
    android:updatePeriodMillis="4000000"
    android:initialLayout="@layout/appwidgetlayout">
    <!-- 从上到下
    	宽
    	高
    	更新毫秒数
    	appwidget的初始化的布局 引用 res/layout 中的布局
     -->    
</appwidget-provider>

3.实现一个类继承 AppWidgetProvider 

public class AppWeigetTest extends AppWidgetProvider {
	private static final String in="joker.broadcast.appwedget";
	//当到达指定的更新时间之后,或用户添加窗口小部件时触发
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onUpdate(context, appWidgetManager, appWidgetIds);		
	}
	//当用户删除窗体部件时触发
	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		// TODO Auto-generated method stub	
	}
	//当所有船体部件删除时触发
	@Override
	public void onDisabled(Context context) {
		// TODO Auto-generated method stub
	}
	//接收到广播的时候触发,以上几个方法是使用这个方法转发的,也就是说其实窗口的各种"事件" 都是使用广播的形式发出的
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		super.onReceive(context, intent);
	}
}
4.在AndroidManifest.xml

 <receiver android:name=".AppWeigetTest">
             <intent-filter>
                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>                 
             </intent-filter>
             <meta-data android:name="android.appwidget.provider"
                 android:resource="@xml/appwidget_info"/>
         </receiver>
以上 intent-filter 和 meta-data是系统设置不变的,其中meta-data 的resource 下的文件是 appwidget的配置文件

二, Appwidget中常用类的使用:

1.PendingIntent  

           此类是包装Intent类的 使Intent 在特定的事件触发    

PendingIntent包装Intent意图 初始化方法有:getActivity getBroadcast getService
//参1 上下文对象 参数3 Intent对象
PendingIntent pending=PendingIntent.getActivity(context, 0, inten, 0);
2.RemoteViews

         此类可以对:"小部件" 上的控件进行操作(比如改变TextView上的文字ImageView上的图片操作,改变图片添加事件) 

RemoteViews remote=new RemoteViews(context.getPackageName(), R.layout.appwidgetlayout);
			//为窗口小部件上的控件添加点击事件
			//参数一: 指定窗口小部件上的控件ID
			//参数2 :指定触发事件后执行的Intent 的PendingIntent
remote.setOnClickPendingIntent(R.id.button1, pending); //像button1添加一个点击事件
3 AppWidgetManager

在onXXX事件中做为参数传递进来的,此类的对象负责更新Appwidget 

			AppWidgetManager manager=AppWidgetManager.getInstance(context);    
			ComponentName name=new ComponentName(context, AppWeigetTest.class);//代表AppWeiget控件
			//以上两个步骤是在没有appwidgetmanager对象的时候,比如在onReceive方法里如何使用
			manager.updateAppWidget(name, remoteViews); //此方法更新appwidget并且执行remoteViews只有执行这个方法后remoteViews配置的信息才会生效



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Android 笔记本应用的代码示例,包括增加、删除、修改和查找笔记的功能: 1. 在 activity_main.xml 中添加一个 ListView 元素和三个 Button 元素,以便用户列出、添加、编辑和删除笔记: ```xml <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/addButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add"/> <Button android:id="@+id/editButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Edit"/> <Button android:id="@+id/deleteButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Delete"/> ``` 2. 在 note_item.xml 中添加一个 TextView 元素,以便在 ListView 中显示笔记的标题: ```xml <TextView android:id="@+id/titleTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:padding="10dp"/> ``` 3. 在 MainActivity.java 文件中编写以下代码: ```java import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { ListView listView; Button addButton, editButton, deleteButton; ArrayList<String> notes; ArrayAdapter<String> adapter; int selectedIndex = -1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = findViewById(R.id.listView); addButton = findViewById(R.id.addButton); editButton = findViewById(R.id.editButton); deleteButton = findViewById(R.id.deleteButton); notes = new ArrayList<>(); adapter = new ArrayAdapter<>(this, R.layout.note_item, R.id.titleTextView, notes); listView.setAdapter(adapter); addButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showNoteDialog("New note", "", true); } }); editButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (selectedIndex != -1) { String note = notes.get(selectedIndex); showNoteDialog("Edit note", note, false); } else { Toast.makeText(MainActivity.this, "Please select a note to edit", Toast.LENGTH_SHORT).show(); } } }); deleteButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (selectedIndex != -1) { String note = notes.get(selectedIndex); showDeleteDialog(note); } else { Toast.makeText(MainActivity.this, "Please select a note to delete", Toast.LENGTH_SHORT).show(); } } }); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { selectedIndex = i; } }); } private void showNoteDialog(String title, String note, final boolean isNew) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(title); final EditText input = new EditText(this); input.setText(note); builder.setView(input); builder.setPositiveButton("Save", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { String text = input.getText().toString(); if (isNew) { notes.add(text); } else { notes.set(selectedIndex, text); } adapter.notifyDataSetChanged(); } }); builder.setNegativeButton("Cancel", null); builder.show(); } private void showDeleteDialog(final String note) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Delete note"); builder.setMessage("Are you sure you want to delete this note?"); builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { notes.remove(note); adapter.notifyDataSetChanged(); selectedIndex = -1; } }); builder.setNegativeButton("No", null); builder.show(); } } ``` 4. 运行应用程序并测试添加、编辑、删除和查找笔记的功能。 注意:这只是一个简单的笔记本应用程序示例,未进行任何输入验证或错误处理。在实际应用程序中,您可能需要添加更多的代码来确保应用程序的稳定性和安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值