小编Sam*_*awy的帖子

如何在焦点/按下时更改ImageButton的色调

我有一个ImageButton在我的应用程序,我需要在按钮时更改图像的色调pressed/focused.我有一个从XML文件ImageButton获取它的集合,src如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- pressed -->
    <item 
        android:state_pressed="true"
        android:tint="@color/black"
        android:drawable="@drawable/search"
        />

    <!-- focused -->
    <item 
        android:state_focused="true"
        android:tint="@color/black"
        android:drawable="@drawable/search"
        />

    <!-- default -->
    <item
        android:tint="@null"
        android:drawable="@drawable/search"
        />

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

但是,ImageButton按下或聚焦时不会应用色调- 图像只显示正常.黑色一直定义为黑色#000000.有任何想法吗?

android image tint selector imagebutton

57
推荐指数
4
解决办法
9万
查看次数

HttpStatus在API级别22中被弃用

通常我习惯使用类来检查我的RestClient类中的服务器不同的代码响应,org.apache.http.HttpStatus如下例所示:

if (HttpStatus.SC_OK == restClient.getResponseCode()) {
            //200 OK (HTTP/1.0 - RFC 1945)
            return true;
} else if (HttpStatus.SC_BAD_GATEWAY == restClient.getResponseCode()){
           //502 Bad Gateway (HTTP/1.0 - RFC 1945)
            return false;
}
Run Code Online (Sandbox Code Playgroud)

但是根据官方文档,最近该类因API级别22而弃用

此接口在API级别22中已弃用.请改用openConnection().请访问此网页了解更多详情.

OpenConnection()对我来说使用方法没有任何意义.

我的问题是:有没有其他方法可以执行相同的功能而无需在应用程序中自行对所有代码响应进行硬编码?

提前致谢.

android http rest-client

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

Android:更新后重启应用程序 - ACTION_PACKAGE_REPLACED

我的应用程序不在Play商店上验证在网络上如果有新版本并下载并启动它.安装完毕后,我想重新启动应用程序,我会用一个BroadcastRecevierACTION_PACKAGE_REPLACED.这是代码:

广播:

public void onReceive(Context context, Intent intent) {
  if(intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)){
    ApplicationInfo app = new ApplicationInfo();
    if(app.packageName.equals("it.android.downloadapk")){
      Intent LaunchIntent = context.getPackageManager().getLaunchIntentForPackage(app.packageName);
      context.startActivity(LaunchIntent);                    
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

表现:

<receiver android:name="it.android.downloadapk.Broadcast">
  <intent-filter>
    <action android:name="android.intent.action.ACTION_PACKAGE_REPLACED"></action>
    <data android:scheme="package" android:path="it.android.downloadapk" /> 
  </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

问题是,当我安装新的apk时,广播似乎没有启动,为什么?

android broadcast

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

排球图像缓存

我试图了解Volley的图像缓存.我在里面有一个带有gridview的片段,它将加载大约12-30个图像.从服务器检索图像,我使用NetworkImageView加载这些图像.

我能够在NetworkImageView中显示图像,一切正常.但是,当我从一个改变fragment到另一个并回到之前的片段时LogCat,我看到Volley正试图再次获取图像.

我读Volley自动处理图像缓存.当图像缓存在第一个时fragment,为什么它会再次尝试获取图像,当我从第二个到第一个回来时?首先,LogCat数据是否显示了Volley的图像请求?或者是其他东西...

以下是我的代码:

onCreate()

queue = Volley.newRequestQueue(getActivity());
imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
    private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(
            10);

    public void putBitmap(String url, Bitmap bitmap) {
        mCache.put(url, bitmap);
    }

    public Bitmap getBitmap(String url) {
        return mCache.get(url);
    }
});
Run Code Online (Sandbox Code Playgroud)

第一次加载片段时的Logcat:

02-18 14:21:20.724: D/Volley(14713): [4944] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://xx.files.wordpress.com/2014/02/screen-shot-2014-02-17-at-11-57-29-pm.png 0x800c5bdc LOW 2> [lifetime=3782], [size=398563], [rc=200], [retryCount=0]
02-18 14:21:20.874: D/Volley(14713): [4943] BasicNetwork.logSlowRequests: HTTP response for …
Run Code Online (Sandbox Code Playgroud)

android caching image android-fragments android-volley

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

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'android.view.View .MainActivity.findViewById(int)'

我有一个叫做MainActivity.javaAsyncTask类来调用一个类.最后一个类findViewById()在执行时返回此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View <mypackage>.MainActivity.findViewById(int)' on a null object reference
Run Code Online (Sandbox Code Playgroud)

我不明白如何ImageView在完成工作R.layout.activity_main之后编辑定位AsyncTask.

MainActivity.java

public class MainActivity extends Activity {

    public MainActivity() {}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Connection().execute();
    }

}
Run Code Online (Sandbox Code Playgroud)

Connection.java

public class Connection extends AsyncTask<String, Void, String> {
    public String result;

    //I know, this isn't correct, how can i do?
    public MainActivity MainActivity;

    @Override
    protected String doInBackground(String... arg0) {
        // …
Run Code Online (Sandbox Code Playgroud)

java android android-asynctask

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

放置API密钥的位置?资源,清单中的元数据还是静态变量?

我想弄清楚将API密钥添加到我的Android应用程序中的最佳做法是什么.

- 有些人建议meta-dataManifest.xml文件中使用- 链接.这让我觉得,最好在resources文件中添加密钥.

<meta-data android:value="key_value" android:name="api_key"></meta-data>
Run Code Online (Sandbox Code Playgroud)

- 有人说,把它添加到resources- 链接

<string name="api_key">api_key_value</string">
Run Code Online (Sandbox Code Playgroud)

- 我们可以在类代码中简单地添加它

api.configue("api_key_value");
Run Code Online (Sandbox Code Playgroud)

- 有些人说添加密钥Manifest.xmlresources文件将允许其他应用程序读取它 - 链接.

<string name="foo">bar</string">
Run Code Online (Sandbox Code Playgroud)

我并不是想找到最安全的方法,因为对我来说,我会将Key保存在服务器中并在运行时检索它.

我问的是最好的方法和最佳实践.

提前致谢.

android android-manifest android-studio

9
推荐指数
1
解决办法
5486
查看次数

OnBackPressed没有被调用?

我已经覆盖了OnBackPressed我的活动中的函数,但它没有被调用.在其他活动上它工作正常.这是我的方法:

@Override
public void onBackPressed() {
    Log.e("back",""+1); 
    UserPage.getstate().finish();
    Intent i=new Intent(CreateGroup.this,UserPage.class);
    i.putExtra("title11","dd");
    startActivity(i);
    finish();
}
Run Code Online (Sandbox Code Playgroud)

此方法未被调用,OnBackPresssed每次按后退按钮时都会调用默认值.

android

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

FragmentManager替换片段而不为旧片段调用onPause()onDestroy()

在我的应用程序中,我有一个左侧菜单控制我的Fragment一个FragmentView中的事务MainActivity.在FragmentView我之上,PullToRefresh新GMAIL中使用的新组件仅在一个中刷新Fragment.

在此输入图像描述

如果我Fragment用另一个替换它,我想停止刷新它.

getSupportFragmentManager().beginTransaction()
            .replace(R.id.fragment_container, new pullToRefreshFragment())
            .commit();
Run Code Online (Sandbox Code Playgroud)

我预计,FragmentManager固守传统FragmentLifeCycle然后,我决定停止PullToRefresh在部件onPause,onDestroy,onStoponDetach()方法.不幸的是,那些方法从来没有被称为替换Fragment.如果我从左侧菜单中选择了相同的片段,它们只会被替换.

@Override
public void onDetach() {
    if(mPullToRefreshLayout.isRefreshing()){
        mPullToRefreshLayout.setRefreshComplete();
    }
    super.onDetach();
}
Run Code Online (Sandbox Code Playgroud)

这意味着,FragmentManager缓存片段并不会破坏它们,除非我试图用同一类型的另一个片段替换片段.

我的问题是,在使用FragmentManager?替换或添加新片段时,片段lifeCycle是什么?

我应该在哪里停止我的PullToRerfesh组件,如果我想在用另一个片段替换片段时停止它?

android android-fragments pull-to-refresh fragmentmanager

5
推荐指数
0
解决办法
1083
查看次数

如何使customAdapter填充gridview高度

我正在使用Sudoku app.布局是9x9 GridView.每个GridView都包含customAdapter的9个Textview.我想让9个TextViews填充每个GridView的高度.怎么样?

这就是我现在所拥有的 在此输入图像描述

这是myGridAdapter:

public class myGridAdapter extends BaseAdapter {

private Context context;
private String[] mobileValues;

public myGridAdapter(MainActivity mainActivity, String[] arrayEmpty) {
    this.context = mainActivity;
    this.mobileValues = arrayEmpty;
}

public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    if (convertView == null) {

        gridView = new View(context);

        // get layout from text_item.xml
        gridView = inflater.inflate(R.layout.text_item, null);

        // set value into textview
        TextView textView = (TextView) gridView
                .findViewById(R.id.grid_item_label);
        textView.setText(mobileValues[position]);

    } else { …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-gridview

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

使用cin检查空行

我想检查空行作为执行特定操作的输入.我试图使用cin.peek()并检查它是否等于'\n',但它没有意义.

一个

b

C

空行(这里,我想执行我的动作)

一个

我试过这段代码:

char a,b,c;
cin>>a;
cin>>b;
cin>>c;
if(cin.peek()=='\n') {
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
}
Run Code Online (Sandbox Code Playgroud)

c++

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

毕加索导致任务执行失败':app:dexDebug'

我正在使用Android工作室,我正在尝试将Picasso库添加到我的项目中,但每当我尝试运行应用程序时,我都会遇到此错误

Error:Execution failed for task ':app:dexDebug'.
com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2
Run Code Online (Sandbox Code Playgroud)

这是我的Gradle档案

apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "22.0.0"

defaultConfig {
    applicationId "com.example"
    minSdkVersion 15
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.facebook.android:facebook-android-sdk:3.21.1'
//TODO::Update to the latest version of Play Service
compile 'com.google.android.gms:play-services:6.5.87'
compile 'com.lorentzos.swipecards:library:1.0.8'
compile …
Run Code Online (Sandbox Code Playgroud)

android android-studio build.gradle picasso android-gradle-plugin

0
推荐指数
1
解决办法
1848
查看次数