首先,我是因为我的英语很糟糕而感到害羞 我是android的新手。我有一个问题,我想在Android上的计算器小部件但是 我的代码不起作用。 我首先应用按钮只点击一个idname为buttun1的按钮。 我的代码请帮助我。 感谢。
package widgetcalculator;
import com.example.widgetcalculator.R;,
public class Calculator extends AppWidgetProvider {
RemoteViews rmViews;
ComponentName cmName;
AppWidgetManager appWidgetManager;
public static String WIDGET_BUTTON = "com.example.widgetcalculator.button1";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
rmViews = new RemoteViews(context.getPackageName(),R.layout.main);
appWidgetManager = this.appWidgetManager;
Intent intent = new Intent(WIDGET_BUTTON);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
rmViews.setOnClickPendingIntent(R.id.button1, pendingIntent );
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
RemoteViews rmViews=new RemoteViews(context.getPackageName(),R.layout.main);
if (WIDGET_BUTTON.equals(intent.getAction())) {
//your code here
rmViews.setTextViewText(R.id.textView1, "1");
cmName = new ComponentName(context, Calculator.class);
appWidgetManager.updateAppWidget(cmName, rmViews);
}
}
}
和我的mainfest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.widgetcalculator"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".Calculator" android:label="@string/app_name">
<intent-filter>
<action android:name="com.example.widgetcalculator.button1" />
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />,
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" />
</receiver>
</application>
和我的layout / main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="0"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:id="@+id/buttontop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:id="@+id/buttoncik"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9" />
<Button
android:id="@+id/buttoncarp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/buttonsil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C" />
<Button
android:id="@+id/button0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<Button
android:id="@+id/buttonnokta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."
android:textStyle="bold" />
<Button
android:id="@+id/buttonbol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="%" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:2)
public class WidgetProvider extends AppWidgetProvider {
//action for buttons
public static final String ACTION_WIDGET_PLAY_PAUSE = "btn_play_pause";
public static final String ACTION_WIDGET_STOP = "btn_stop";
public static final String ACTION_WIDGET_PREV_TRACK = "btn_prev_track";
public static final String ACTION_WIDGET_NEXT_TRACK = "btn_next_track";
public static final String ACTION_WIDGET_MUTE = "btn_mute";
public static final String ACTION_WIDGET_VOL_UP = "btn_vol_up";
public static final String ACTION_WIDGET_VOL_DOWN = "btn_vol_down";
public static final String ACTION_WIDGET_EXPAND_WIDGET = "btn_transparent_activity";
// <Action, ButtonId>
private static final HashMap<String, Integer> CLICK_INTENT_ACTIONS_MAP = new HashMap<String, Integer>();
// <Actions>
private static final ArrayList<String> ACTIONS = new ArrayList<String>();
static {
CLICK_INTENT_ACTIONS_MAP.put(ACTION_WIDGET_PLAY_PAUSE, R.id.btn_media_play_pause);
CLICK_INTENT_ACTIONS_MAP.put(ACTION_WIDGET_STOP, R.id.btn_media_stop);
CLICK_INTENT_ACTIONS_MAP.put(ACTION_WIDGET_PREV_TRACK, R.id.btn_media_previous);
CLICK_INTENT_ACTIONS_MAP.put(ACTION_WIDGET_NEXT_TRACK, R.id.btn_media_next);
CLICK_INTENT_ACTIONS_MAP.put(ACTION_WIDGET_MUTE, R.id.btn_media_mute);
CLICK_INTENT_ACTIONS_MAP.put(ACTION_WIDGET_VOL_UP, R.id.btn_media_vol_up);
CLICK_INTENT_ACTIONS_MAP.put(ACTION_WIDGET_VOL_DOWN, R.id.btn_media_vol_down);
CLICK_INTENT_ACTIONS_MAP.put(ACTION_WIDGET_EXPAND_WIDGET, R.id.btn_show_activity);
// Fill action list
ACTIONS.addAll(CLICK_INTENT_ACTIONS_MAP.keySet());
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent activeIntent;
PendingIntent actionPendingIntent;
int mapSize = CLICK_INTENT_ACTIONS_MAP.size();
// Fill btn id list
ArrayList<Integer> btnIds = new ArrayList<Integer>();
btnIds.addAll(CLICK_INTENT_ACTIONS_MAP.values());
// Set button listeners
for (int i = 0; i < mapSize; i++) {
activeIntent = new Intent(context, WidgetProvider.class);
activeIntent.setAction(ACTIONS.get(i));
actionPendingIntent = PendingIntent.getBroadcast(context, 0, activeIntent, 0);
remoteViews.setOnClickPendingIntent(btnIds.get(i), actionPendingIntent);
}
//Update
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
context.startService(new Intent(context.getApplicationContext(), AppControllerService.class));
}
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (ACTIONS.contains(action)) {
int btnId = CLICK_INTENT_ACTIONS_MAP.get(action);
switch (btnId) {
case R.id.btn_media_play_pause:
case R.id.btn_media_stop:
case R.id.btn_media_previous:
case R.id.btn_media_next:
case R.id.btn_media_mute:
case R.id.btn_media_vol_up:
case R.id.btn_media_vol_down:
break;
case R.id.btn_show_activity:
Intent i = new Intent(context, ActivityMain.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
break;
default:
break;
}
}
super.onReceive(context, intent);
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
}
}