我是一个菜鸟,并且正在努力应该做一些相对简单的事情。
我创建了一个包含8个TextView的小部件,当Widget被初始化时,这个小部件由带有文本字段的config活动填充。所有这一切都运行良好,但我希望用户能够在初始化时进一步自定义小部件。
我在每个文本字段旁边放置了一个复选框,用于使每个字段可见(选中)或隐藏(未选中),但我很难过如何让这些复选框将textviews设置为在窗口小部件类中可见或隐藏。
配置布局XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvwelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/widget_config_title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tvinstructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tvwelcome"
android:text="@string/widget_config_info"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="@+id/bwidgetconfig"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="@string/widget_config_finishbutton" />
<EditText
android:id="@+id/etemail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/bwidgetconfig"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/etphone"
android:layout_marginBottom="25dp"
android:ems="10"
android:hint="@string/widget_config_emailhint"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/etphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/etemail"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/cbPhone"
android:ems="10"
android:hint="@string/widget_config_phonehint"
android:inputType="phone" />
<EditText
android:id="@+id/etaddress4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/etphone"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/etaddress3"
android:ems="10"
android:hint="@string/widget_config_address4hint"
android:inputType="textPostalAddress"
android:maxLines="1" />
<EditText
android:id="@+id/etaddress3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/etaddress4"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/etaddress2"
android:ems="10"
android:hint="@string/widget_config_address3hint"
android:inputType="textPostalAddress"
android:maxLines="1" />
<EditText
android:id="@+id/etaddress2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/etaddress3"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/etaddress1"
android:ems="10"
android:hint="@string/widget_config_address2hint"
android:inputType="textPostalAddress"
android:maxLines="1" />
<EditText
android:id="@+id/etaddress1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/etaddress2"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/etname"
android:ems="10"
android:hint="@string/widget_config_address1hint"
android:inputType="textPostalAddress"
android:maxHeight="10dp"
android:maxLines="1" />
<EditText
android:id="@+id/etname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/etaddress1"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/cbName"
android:ems="10"
android:hint="@string/widget_config_namehint"
android:inputType="textPersonName"
android:maxHeight="10dp" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/etcontext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/etname"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:ems="10"
android:hint="@string/widget_config_contexthint"
android:maxHeight="10dp"
android:maxLines="1"
android:textSize="15sp" />
<CheckBox
android:id="@+id/cbName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/etaddress1"
android:layout_alignParentRight="true"
android:layout_below="@+id/etcontext"
android:text="@string/widget_config_checkbox"
android:onClick="onCheckboxClicked" />
<CheckBox
android:id="@+id/cbAddress1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/etaddress2"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/etaddress1"
android:text="@string/widget_config_checkbox"
android:onClick="onCheckboxClicked" />
<CheckBox
android:id="@+id/cbAddress2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/etaddress3"
android:layout_alignParentRight="true"
android:layout_below="@+id/cbAddress1"
android:text="@string/widget_config_checkbox"
android:onClick="onCheckboxClicked" />
<CheckBox
android:id="@+id/cbAddress3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/etaddress4"
android:layout_alignParentRight="true"
android:layout_below="@+id/cbAddress2"
android:text="@string/widget_config_checkbox"
android:onClick="onCheckboxClicked" />
<CheckBox
android:id="@+id/cbAddress4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/etphone"
android:layout_alignParentRight="true"
android:layout_below="@+id/cbAddress3"
android:text="@string/widget_config_checkbox"
android:onClick="onCheckboxClicked" />
<CheckBox
android:id="@+id/cbPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/etemail"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/etphone"
android:text="@string/widget_config_checkbox"
android:onClick="onCheckboxClicked" />
<CheckBox
android:id="@+id/cbEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/etemail"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/etemail"
android:text="@string/widget_config_checkbox"
android:onClick="onCheckboxClicked" />
</RelativeLayout>
小组件配置类:
package com.###.###;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RemoteViews;
public class WidgetConfig extends Activity implements OnClickListener{
EditText infocontext;
EditText infoname;
EditText infoaddress1;
EditText infoaddress2;
EditText infoaddress3;
EditText infoaddress4;
EditText infophone;
EditText infoemail;
AppWidgetManager awm;
Context c;
int awID;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.widgetconfig);
Button b = (Button)findViewById(R.id.bwidgetconfig);
b.setOnClickListener(this);
c = WidgetConfig.this;
infocontext = (EditText)findViewById(R.id.etcontext);
infoname = (EditText)findViewById(R.id.etname);
infoaddress1 = (EditText)findViewById(R.id.etaddress1);
infoaddress2 = (EditText)findViewById(R.id.etaddress2);
infoaddress3 = (EditText)findViewById(R.id.etaddress3);
infoaddress4 = (EditText)findViewById(R.id.etaddress4);
infophone = (EditText)findViewById(R.id.etphone);
infoemail = (EditText)findViewById(R.id.etemail);
//Getting info about the widget that launched this Activity
Intent i = getIntent();
Bundle extras = i.getExtras();
if (extras !=null){
awID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID
);
}else{
finish();
}
awm = AppWidgetManager.getInstance(c);
}
@Override
public void onClick(View v) {
String str_context = infocontext.getText().toString();
String str_name = infoname.getText().toString();
String str_address1 = infoaddress1.getText().toString();
String str_address2 = infoaddress2.getText().toString();
String str_address3 = infoaddress3.getText().toString();
String str_address4 = infoaddress4.getText().toString();
String str_phone = infophone.getText().toString();
String str_email = infoemail.getText().toString();
RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget);
views.setTextViewText(R.id.tvContext, str_context);
views.setTextViewText(R.id.tvName, str_name);
views.setTextViewText(R.id.tvAddress1, str_address1);
views.setTextViewText(R.id.tvAddress2, str_address2);
views.setTextViewText(R.id.tvAddress3, str_address3);
views.setTextViewText(R.id.tvAddress4, str_address4);
views.setTextViewText(R.id.tvPhone, str_phone);
views.setTextViewText(R.id.tvEmail, str_email);
awm.updateAppWidget(awID, views);
Intent result = new Intent();
result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
setResult(RESULT_OK, result);
finish();
}
}
小工具类: package com。###。###;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.widget.RemoteViews;
import android.widget.Toast;
public class Widget extends AppWidgetProvider{
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++){
int awID = appWidgetIds[i];
RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.widget);
appWidgetManager.updateAppWidget(awID, v);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
Toast.makeText(context, "Widget deleted from Homescreen", Toast.LENGTH_SHORT).show();
}
}
如何使用复选框隐藏/显示文本视图的任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
您可以列出复选框和文本视图列表,单击复选框后,您可以在列表中获取其位置,然后使用textview.setVisibility(View.VISIBLE)或View.GONE.Don't use View.INVISIBLE。
您可以设置每个复选框的标记及其位置,稍后您将使用getTag来了解复选框的索引