小编Cod*_*ife的帖子

如何从资源ID获取资源名称


在我的布局中,我已经定义了类似的东西.

<RadioButton
    android:id="@+id/radio1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Dnt want this text" />
Run Code Online (Sandbox Code Playgroud)

假设活动中的某些函数返回此id(radioButton的id).现在我想从这个id 得到这个文本radio1.总之,我想要检索写入的文本radio1android:id="@+id/radio1"

有人能告诉我怎么可能?

android

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

Retrofit 2.x:请求和响应的日志标头

我正在使用retrofit 2.x,我想记录请求和响应的标题和正文.

  HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(interceptor)
            .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
            .addNetworkInterceptor(new Interceptor() {
                @Override
                public okhttp3.Response intercept(Chain chain) throws IOException {
                    Request request = chain.request().newBuilder()
                            .addHeader("key", "value")
                            .addHeader("HEADER","HEADER Value")
                            .build();
                    return chain.proceed(request);
                }


            }).build();
Run Code Online (Sandbox Code Playgroud)

这就是我正在做的,我的问题是请求的标题没有记录在Android监视器,但休息一切都记录下来.

Gradle版本

 compile ('com.squareup.retrofit2:retrofit:2.0.0-beta3') {
    // exclude Retrofit’s OkHttp peer-dependency module and define your own module import
    exclude module: 'okhttp'
}
compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta3'
compile ('com.squareup.okhttp3:logging-interceptor:3.0.1'){
    exclude module: 'okhttp'
}
Run Code Online (Sandbox Code Playgroud)

由于bug问题使用RC1和3.0.1报告了Bug Link

android android-networking retrofit retrofit2

12
推荐指数
3
解决办法
5810
查看次数

单击时禁用按钮

我是编程世界的新手,我的知识有限.如果我有任何错误,请原谅.我的问题是.

我正在创建一个具有START&STOP按钮的Activity.当用户点击START按钮时,必须启动服务; 停止服务必须停止.

现在我想在我点击开始按钮(点击开始按钮时服务开始)和点击停止按钮时禁用我的开始按钮我希望看到开始按钮作为正常的可点击按钮.

我通过创建按钮对象使用了.setEnabled(false).我需要帮助...先谢谢

android android-widget

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

避免 kotlin 中“When”中的 else 条件

根据When in Kotlin 的文档,如果编译器知道所有值都被覆盖,则 else 不是强制性的。对于 emum 或密封类来说这是非常重要的,但是对于数字 1 到 5 的数组(startRating)如何做到这一点。

private fun starMapping(startRating: Int): String {

    return when (startRating) {
        1 -> "Perfect"
        2 -> "Great"
        3-> "Okay"
        4-> "Bad"
        5-> "Terrible"
        // don't want to add else as I believe it is prone to errors.
    }
}
Run Code Online (Sandbox Code Playgroud)

与此类似的东西

return when (AutoCompleteRowType.values()[viewType]) {
        AutoCompleteRowType.ITEM -> ItemView(
                LayoutInflater.from(parent.context).inflate(R.layout.item_venue_autocomplete_item_info, parent, false))

        AutoCompleteRowType.SECTION -> SectionView(
                LayoutInflater.from(parent.context).inflate(R.layout.item_venue_autocomplete_section, parent, false)
        )
    }
Run Code Online (Sandbox Code Playgroud)

kotlin kotlin-when

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

在触摸时画圆圈

我想画一个用户触摸屏幕的圆圈.

public class MainActivity extends Activity implements OnTouchListener {

LinearLayout layout;
float x=0;
float y=0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_main);
    layout=(LinearLayout)findViewById(R.id.layout);
    layout.addView(new CustomView(MainActivity.this));
}

public class CustomView extends View {

    Bitmap mBitmap;
    Paint paint;
    public CustomView(Context context) {
        super(context);
        mBitmap = Bitmap.createBitmap(400, 800, Bitmap.Config.ARGB_8888);
        paint=new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.setBitmap(mBitmap);
        canvas.drawCircle(x, y, 50, paint);
        Toast.makeText(MainActivity.this, "DDD", 1).show();
    }
}

@Override
public boolean onTouch(View v, MotionEvent event) {

    if (event.getAction() == …
Run Code Online (Sandbox Code Playgroud)

android android-layout

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

设置错误Android

任何人都可以告诉我如何适当地使用SetError函数CheckBoxes和RadioButtons.我能够在textView之后引发错误图标符号(!)但看不到错误消息.根据Android的文档: 在复选框a和radioButton的情况下,你能否提供一个正确实现setError的例子?这是我的代码 -
sets the right-hand compound drawable of the TextView to the "error" icon and sets an error message that will be displayed in a popup when the TextView has focus. The icon and error message will be reset to null when any key events cause changes to the TextView's text. If the error is null, the error message and icon will be cleared.


CheckBox box=(CheckBox)findViewById(R.id.propertyStatus1);
box.setError("Error");
Run Code Online (Sandbox Code Playgroud)

android android-layout

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

保存活动状态

我有两个活动A,B.现在从A i按下按钮(使用startActivity())调用B ,然后按返回键返回到A. 现在当我再次按下按钮转到B时,会调用新的活动(如预期的那样).

现在有人能告诉我如何显示旧的B状态吗?

我已经阅读了这篇文章 使用保存实例状态保存Android活动状态,但无法帮助自己:(

public class B extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main1);

    if(savedInstanceState!=null){
        EditText editText=(EditText)findViewById(R.id.editText1);
        editText.setText(savedInstanceState.getString("EditBox"));
    }
}

@Override
protected void onSaveInstanceState(Bundle onSaveInstanceState) {
    System.out.println("B.onSaveInstanceState()");
    super.onSaveInstanceState(onSaveInstanceState);
    onSaveInstanceState.putString("EditBox","Hello");
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    System.out.println("B.onRestoreInstanceState()");
    super.onRestoreInstanceState(savedInstanceState);
    EditText editText=(EditText)findViewById(R.id.editText1);
    editText.setText(savedInstanceState.getString("EditBox"));
}}
Run Code Online (Sandbox Code Playgroud)

我的A级

public class A extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button button=(Button)findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() …
Run Code Online (Sandbox Code Playgroud)

android workflow-activity android-activity

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

如何使用ProgressDialog和Async Task同时获取Method

我有通用的异步任务类,它从服务器获取响应.我通过使用get方法接收这些响应.现在,当我使用get方法时,我知道UI线程是块,bcoz我的进度Dialog没有按时显示.

现在可以有人告诉我替代做这个吗?(在每种情况下,我都需要将响应发送回执行调用的活动,因此打开新活动对我没有帮助)

代码:AsyncTask类

 public class GetDataFromNetwork extends AsyncTask<Void,String,Object> {

protected void onPreExecute() {
    super.onPreExecute();
    progressDialog.show();
} 


 protected Object doInBackground(Void... params) {
    Object result = null;
    try {
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
        new MarshalBase64().register(envelope);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(ipAddress + webService);
        System.setProperty("http.keepAlive", "true");
        try {
            androidHttpTransport.call(nameSpace + methodName, envelope);
        } catch (Exception e) {

            e.printStackTrace();
            publishProgress(e.getMessage());
        }
        androidHttpTransport.debug = true;
        System.out.println("response: " + androidHttpTransport.requestDump);
        result = envelope.getResponse();
        if(result!=null){
            System.out.println("GetDataFromNetwork.doInBackground() result expection---------"+result);
        }
    } catch (Exception e) …
Run Code Online (Sandbox Code Playgroud)

android progressdialog android-asynctask

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