小编Sam*_*Sam的帖子

如何根据x的值更改文本输出

我想创建一到十个列表.每次循环它添加一个x,我也希望它像这样打印.

1
 2
  3
   4
    5
     6
      7
       8
        9
         10
Run Code Online (Sandbox Code Playgroud)

现在它打印像这样:

1
2
3
4
5
6
7
8
9
10
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
184
查看次数

删除String中的前导括号和尾随括号

在Java中,我想摆脱String中的前导括号和尾随括号.
给定输入:

"[ hello, char[]={a,b,c} ... bye ]"
Run Code Online (Sandbox Code Playgroud)

我怎样才能产生输出

" hello, char[]={a,b,c} ... bye "
Run Code Online (Sandbox Code Playgroud)

只删除了前导[和尾随]...我怎么能用Java做到这一点?

java string substring

2
推荐指数
3
解决办法
2万
查看次数

如何在ListView中为单个元素设置动画

我正在做一个基于ListView的小型Android应用程序.当用户选择列表中的一个或多个元素并随后从ActionBar中选择一个菜单项时,我想对列表中的选定元素执行一个小动画,这就是出错的地方.

什么都没有动画 - 也没有任何失败.以下代码片段是我正在做的简化版本:

private void animateListViewItem()
{
    TranslateAnimation anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f);
    anim.setDuration(2000);
    View v = fragment.getListAdapter().getView(fragment.getListView().getFirstVisiblePosition(), null, null);
    v.startAnimation(anim);
}
Run Code Online (Sandbox Code Playgroud)

当我搞砸了它,试图弄清楚什么是错的时候,我在某一点上用整个ListView替换了项目以排除动画作为问题的根源 - 就像这样.

private void animateListViewItem()
{
    TranslateAnimation anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f);
    anim.setDuration(2000);
    fragment.getListView().startAnimation(anim);
}
Run Code Online (Sandbox Code Playgroud)

令我惊讶的是,这完美无缺!

所以我的问题是 - 为什么我不能动画ListView中的各个元素?或者我做错了什么?

谢谢!

PS为了记录,ListView填充了自定义视图(LinearLayouts),我已经检查过我在动画制作之前得到了正确的项目.

animation android listview

2
推荐指数
1
解决办法
2837
查看次数

如何在网格布局中正确对齐图像下方的文本,并在超出图像宽度时使其占据第二行?

我的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/signOut"
        android:layout_centerInParent="true"
        android:layout_marginLeft="10dip"
        />

    <TextView
        android:id="@+id/gridtext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageMenu"
        android:layout_marginLeft="10dip"
        android:layout_marginTop="10dip"
        android:layout_centerHorizontal="true"
        android:text="TextView"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我可以在此网格布局中将图像设置在图像的正下方,但是如果文本太大,我还希望将文本包装到下一行.例如,我有一个"血库"的图像,文字是"最近的血库".我希望它包含在图像的宽度内.怎么做?

android textview android-layout android-imageview

2
推荐指数
1
解决办法
9346
查看次数

按下时按钮android

我想在我的按钮被按下时打印一些东西(而不是在它被释放之后)。目前我有这个,但它只能工作一次......

button.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            System.out.println("pressed");
            return true;
        }
        return false;
    }
});
Run Code Online (Sandbox Code Playgroud)

android android-widget

2
推荐指数
1
解决办法
1万
查看次数

为什么我需要在 Android 中扩展 Activity?

这是一个关于基础的问题:为什么我需要声明一个类public class MainActivity extends Activity

android

2
推荐指数
1
解决办法
2万
查看次数

按钮setOnClickListener上的NullPointerException

sample = (Button) findViewById(R.id.sampleButton);
sample.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // TODO Auto-generated method stub
    }
});
Run Code Online (Sandbox Code Playgroud)

当我尝试加载我的Activity时,它会在Listener上抛出一个空指针异常.为什么?我已全局定义了我的按钮变量.

Button sample;
Run Code Online (Sandbox Code Playgroud)

android android-intent

2
推荐指数
1
解决办法
604
查看次数

android map v2 gms在onInfoWindowClick中启动Intent

我尝试启动一个Intent onInfoWindowClick,但它冻结了:

@Override
public void onInfoWindowClick(Marker marker) {
    TabGroupActivity parentActivity = (TabGroupActivity) getParent();
    Context context = parentActivity.getApplicationContext();
    Intent myIntent = new Intent(context, HouseDetailActivity.class);

    parentActivity.startChildActivity("DetailActivity", myIntent);
}
Run Code Online (Sandbox Code Playgroud)

如果我尝试运行相同的代码

public boolean onMarkerClick(final Marker marker) 
Run Code Online (Sandbox Code Playgroud)

它完美地运作.

maps android

2
推荐指数
1
解决办法
925
查看次数

使用Sharedpreferences检索值

我是Android的新手,我不知道如何使用sharedpreferences来检索存储的值,我找不到任何关于使用这些值在其他活动上编写的示例.如果有人可以帮助我,我会非常感激

这就是我存储数据的方式:

protected void onResume() {
    super.onResume();
    SharedPreferences prefs = getPreferences(0);
    String restoredText1 = prefs.getString("cpw", "30");
    if (restoredText1 != null) {
        savedcostperworker.setText(restoredText1, TextView.BufferType.EDITABLE);

        int selectionStart = prefs.getInt("selection-start", -1);
        int selectionEnd = prefs.getInt("selection-end", -1);
        if (selectionStart != -1 && selectionEnd != -1) {
            savedcostperworker.setSelection(selectionStart, selectionEnd);
        }
    }
}

protected void onPause() {
    super.onPause();
    SharedPreferences.Editor editor = getPreferences(0).edit();
    editor.putString("cpw", savedcostperworker.getText().toString());
    editor.putInt("selection-start", savedcostperworker.getSelectionStart());
    editor.putInt("selection-end", savedcostperworker.getSelectionEnd());
}

private EditText savedcostperworker;
Run Code Online (Sandbox Code Playgroud)

现在我想在另一个活动的TextView中显示值"cpw",但我不知道如何

java android store sharedpreferences

2
推荐指数
1
解决办法
145
查看次数

TextView to String

我正在尝试将TextView转换为字符串,我不确定我是否正确地使用它,下面是我认为可以工作的代码,但我一直在收到错误.

TextView test1 = (TextView)findViewById(R.id.result1);
String testA = test1.getText().toString();
Run Code Online (Sandbox Code Playgroud)

这是正确的方法吗?还是有另一种方法?任何帮助,将不胜感激.

java string android textview

2
推荐指数
1
解决办法
2192
查看次数