Java 框选多个文本视图在小部件中不工作
我已经在appwidget中的单个textview上实现了标记效果。但每当我再添加一个textview时。它只适用于最后一个文本视图。有谁能给我一个解决办法吗。我想选取两个平行的textviewJava 框选多个文本视图在小部件中不工作,java,android,Java,Android,我已经在appwidget中的单个textview上实现了标记效果。但每当我再添加一个textview时。它只适用于最后一个文本视图。有谁能给我一个解决办法吗。我想选取两个平行的textview <TextView android:id="@+id/textMarqueName" android:layout_width="match_parent" android:layout_height="wrap_content"
<TextView
android:id="@+id/textMarqueName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@android:color/black"
android:duplicateParentState="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:paddingBottom="0dp"
android:paddingTop="0dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="@string/textMarque"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" >
<requestFocus
android:duplicateParentState="true"
android:focusable="true"
android:focusableInTouchMode="true" />
</TextView>
<TextView
android:id="@+id/textMarqueDiff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textMarqueName"
android:background="@android:color/black"
android:duplicateParentState="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:paddingBottom="0dp"
android:paddingTop="0dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="@string/textMarque"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" >
<requestFocus
android:duplicateParentState="true"
android:focusable="true"
android:focusableInTouchMode="true" />
</TextView>
---------------Here is my code..-----------------
public class Main extends AppWidgetProvider {
// private static String url =
// "http://10.0.2.2:8081/ci-restserver/index.php/api/Test/test/format/json";
private static String url = "http://api.androidhive.info/contacts/";
String htmlData = "<p><h1> This is first line starting asldfkjlasdfjlaskdfjl;aksfj alskjdflkasf </h1></br><h1> and here comes second line agaoin afaskldfjl;aksfj lakdfjlkasjflkjadflkjasl;dfkja ldfjlaskdfjklafjklasdfjlkadfj aklsdfjlkasdf aslkdfj alskdfjlaskdf lkasjdflkjdflj</h1></p>";
private static final String TAG_CURRENCY = "contacts";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
String currencyText = "";
static JSONArray currency = null;
Context _context;
AppWidgetManager _appWidgetManager;
int[] _appWidgetIds;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
_context = context;
_appWidgetManager = appWidgetManager;
_appWidgetIds = appWidgetIds;
startUp();
Log.i("Into the widget", "This is test calling");
}
public void startUp() {
// check for Internet connectivity
ConnectionDetector cd = new ConnectionDetector(_context);
Boolean isInternetPresent = cd.isConnectingToInternet();
if (isInternetPresent) {
Toast.makeText(_context, "Connection Available", Toast.LENGTH_LONG)
.show();
new GetCurrency().execute();
} else {
Toast.makeText(_context, "Connection Problem", Toast.LENGTH_LONG)
.show();
}
}
private class GetCurrency extends AsyncTask<Void, Void, Void> {
RemoteViews updateViews = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
currency = jsonObj.getJSONArray(TAG_CURRENCY);
Log.d("just outsied: ", "currecy asjflaskdfjl asd");
// looping through All Contacts
for (int i = 0; i < currency.length(); i++) {
JSONObject c = currency.getJSONObject(i);
String name = c.getString(TAG_NAME);
currencyText += name;
currencyText += " :- ";
String email = c.getString(TAG_EMAIL);
currencyText += email;
currencyText += " ";
Log.d(String.valueOf(i), "> " + currencyText);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
updateViews = new RemoteViews(_context.getPackageName(),
R.layout.activity_main);
//to update textViews
updateViews.setTextViewText(R.id.textMarqueDiff, currencyText);
updateViews.setTextViewText(R.id.textMarqueName, htmlData);
for (int i = 0; i < _appWidgetIds.length; i++) {
_appWidgetManager
.updateAppWidget(_appWidgetIds[i], updateViews);
}
}
}
}
---------------这是我的密码-----------------
公共类Main扩展了AppWidgetProvider{
//私有静态字符串url=
// "http://10.0.2.2:8081/ci-restserver/index.php/api/Test/Test/format/json”;
专用静态字符串url=”http://api.androidhive.info/contacts/";
字符串htmlData=“这是从asldfkjlasdfjlaskdfjl开始的第一行;aksfj alskjdflkasf这是第二行;aksfj LAKDFJLKASKJFLKJASL;dfkja ldfjlaskdfjklafjklasdfjlkadfj aklsdfjlkasdf aslkdfj ALSKDFJLASKDKDFKDFF LKADFF”;
私有静态最终字符串标记\u CURRENCY=“contacts”;
私有静态最终字符串标记_NAME=“NAME”;
私有静态最终字符串标记\u EMAIL=“EMAIL”;
字符串currencyText=“”;
静态JSONArray货币=null;
语境(Context)语境;;
AppWidgetManager\u AppWidgetManager;
int[]_appWidgetIds;
@凌驾
public void onUpdate(上下文上下文,AppWidgetManager AppWidgetManager,
int[]appWidgetIds){
onUpdate(上下文、appWidgetManager、AppWidgetId);
_上下文=上下文;
_appWidgetManager=appWidgetManager;
_appWidgetIds=appWidgetIds;
启动();
Log.i(“进入小部件”,“这是测试调用”);
}
公共无效启动(){
//检查互联网连接
ConnectionDetector cd=新的ConnectionDetector(_上下文);
布尔值isInternetPresent=cd.isConnectingToInternet();
如果(isInternetPresent){
Toast.makeText(_上下文,“连接可用”,Toast.LENGTH_LONG)
.show();
新建GetCurrency().execute();
}否则{
Toast.makeText(_上下文,“连接问题”,Toast.LENGTH_LONG)
.show();
}
}
私有类GetCurrency扩展异步任务{
RemoteViews updateViews=null;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
ServiceHandler sh=新的ServiceHandler();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url,ServiceHandler.GET);
Log.d(“响应:”、“>”+jsonStr);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
currency=jsonObj.getJSONArray(TAG_currency);
Log.d(“刚刚出炉:,“currecy asjflaskdfjl asd”);
//通过所有触点循环
对于(int i=0;i”+currencyText);
}
}捕获(JSONException e){
e、 printStackTrace();
}
}否则{
Log.e(“ServiceHandler”,“无法从url获取任何数据”);
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
updateViews=新的远程视图(_context.getPackageName(),
R.布局、活动(主要部分);
//更新文本视图的步骤
updateViews.setTextViewText(R.id.textmarkediff,currencyText);
setTextViewText(R.id.textMarqueName,htmlData);
对于(int i=0;i<\u appWidgetIds.length;i++){
_appWidgetManager
.UpdateAppWidgetGet(_AppWidgetId[i],updateViews);
}
}
}
}
//试试这个方法,希望这能帮助你。。。
**activity_main.xml**
**MainActivity.java**
公共类MainActivity扩展了活动{
私用文本视图文本框;
私有TextView textMarqueName;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textMarqueName=(TextView)findViewById(R.id.textMarqueName);
textMarqueDiff=(TextView)findViewById(R.id.textMarqueDiff);
textMarqueDiff.setSelected(真);
textMarqueName.setSelected(true);
}
}
谢谢。但我想在widget上选框。我一个小时都找不到选定的方法和谷歌。你能给我举个小部件的例子吗?这是活动的例子。我已经实现了这一点。但我想要它作为“小部件”。从AppWidgetProvider派生的类。这样地。。公共类Main扩展了AppWidgetProvider{@Override public void onUpdate(上下文,AppWidgetManager,AppWidgetManager,int[]AppWidgetId){super.onUpdate(上下文,AppWidgetManager,AppWidgetId);}我已经添加了我的代码..请检查它。下面的textview选框,而不是上面的。
// Try this way,hope this will help you...
**activity_main.xml**
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textMarqueName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@android:color/black"
android:duplicateParentState="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:paddingBottom="0dp"
android:paddingTop="0dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Demo textview 1 Demo textview 1 Demo textview 1 Demo textview 1 Demo textview 1 Demo textview 1 Demo textview 1 Demo textview 1 Demo textview 1 Demo textview 1 Demo textview 1 Demo textview 1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/textMarqueDiff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/textMarqueName"
android:background="@android:color/black"
android:duplicateParentState="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:paddingBottom="0dp"
android:paddingTop="0dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Demo textview 2 Demo textview 2 Demo textview 2 Demo textview 2 Demo textview 2 Demo textview 2 Demo textview 2 Demo textview 2 Demo textview 2 Demo textview 2 Demo textview 2 Demo textview 2 "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"/>
</RelativeLayout>
**MainActivity.java**
public class MainActivity extends Activity {
private TextView textMarqueDiff;
private TextView textMarqueName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textMarqueName =(TextView) findViewById(R.id.textMarqueName);
textMarqueDiff =(TextView) findViewById(R.id.textMarqueDiff);
textMarqueDiff.setSelected(true);
textMarqueName.setSelected(true);
}
}