【问题标题】:use textView.setText() for linearlayout in PhoneStateListener在 PhoneStateListener 中使用 textView.setText() 进行线性布局
【发布时间】:2015-12-17 20:30:00
【问题描述】:

我有一个类扩展 PhoneStateListener 来监听电话状态,我想显示我的自定义对话框(设计为 XML 布局),它可以正常工作,但是当我尝试调用 textView.setText() 时出现问题在我的自定义对话框中的 textView 的 onCallStateChanged() 方法中,但没有任何反应。你能告诉我怎么做吗?非常感谢!

这是我的 onCallStateChanged 方法和 getCallLog() 方法:

 public void onCallStateChanged(int state, String incomingNumber) {
        if (incomingNumber != null && incomingNumber.length() > 0) {
            _incomingNumber = incomingNumber;
        }
       final WindowManager wm = (WindowManager) _context.getSystemService(Context.WINDOW_SERVICE);
        final LinearLayout ly;

        switch (state){
            case TelephonyManager.CALL_STATE_IDLE: {
                CallLog lastCall = getNewCallLog();
                if(lastCall.get_callDuration() >0) {
                    //I check my value here and it work properly
                    Toast.makeText(_context.getApplicationContext(),Integer.toString(lastCall.get_callDuration()) + "-" +Integer.toString(lastCall.get_callFee()),Toast.LENGTH_LONG).show();
                    WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
                            WindowManager.LayoutParams.WRAP_CONTENT,
                            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                            PixelFormat.TRANSPARENT);

                    params.height = WindowManager.LayoutParams.WRAP_CONTENT;
                    params.width = WindowManager.LayoutParams.WRAP_CONTENT;
                    params.format = PixelFormat.TRANSPARENT;
                    params.gravity = Gravity.TOP;


                    final LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    ly = (LinearLayout) inflater.inflate(R.layout.custom_dialog, null);
                    TextView txtDuration = (TextView)ly.findViewById(R.id.txt_duration);
                    TextView txtCost = (TextView)ly.findViewById(R.id.txt_cost);
                    Button yesBtn = (Button) ly.findViewById(R.id.btn_yes);
                    //Set value for textView here
                    txtDuration.setText(Integer.toString(lastCall.get_callDuration()));
                    txtDuration.setText(Integer.toString(lastCall.get_callDuration()));

                    txtCost.setText(Integer.toString(lastCall.get_callFee()));
                    yesBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            wm.removeView(ly);
                        }
                    });
                    wm.addView(ly, params);
                }
                break;
            }
            case TelephonyManager.CALL_STATE_OFFHOOK:
                break;
            case TelephonyManager.CALL_STATE_RINGING:

                break;
        }

    }
    public CallLog getNewCallLog()
    {
        CallLog _newCall = new CallLog();
        Cursor cursor = this._context.getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI, null, null,
                null, android.provider.CallLog.Calls.DATE + " DESC");
        int number = cursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER);
        int callType = cursor.getColumnIndex(android.provider.CallLog.Calls.TYPE);
        int date = cursor.getColumnIndex(android.provider.CallLog.Calls.DATE);
        int duration = cursor.getColumnIndex(android.provider.CallLog.Calls.DURATION);

        if(cursor.moveToFirst() )
        {
            String _callType = cursor.getString(callType);
            int dircode = Integer.parseInt(_callType);
            if(dircode == android.provider.CallLog.Calls.OUTGOING_TYPE) {
                String _number = cursor.getString(number);
                String _callDate = cursor.getString(date);
                Date _callDayTime = new Date(Long.valueOf(_callDate));
                int _callDuration = cursor.getInt(duration);
                _package.set_callDuration(_callDuration);
                _package.set_callTime(DateTimeManager.get_instance().convertToHm(_callDayTime.toString()));
                _package.set_outGoingPhoneNumber(_number);
                int fee = _package.CalculateCallFee();
                _newCall.set_callDuration(_callDuration);
                _newCall.set_callFee(fee);
                return _newCall;
            }
        }
        return null;
    }

这是我的自定义布局(对话框): Custom layout

【问题讨论】:

    标签: java android android-layout android-view


    【解决方案1】:

    您已经为 txtDuration 和 txtCost 创建了新的文本视图。尝试在您的 Linearlayout 中添加这些文本视图。希望对你有帮助。

    【讨论】:

    • 我做了但什么也没发生
    • 这些文本视图是否可见?你也可以分享一下你的布局吗?
    • 代码看起来不错。你能在 onCallStateChanged() 方法和 switch case 中记录一些东西来检查是否都被调用了。
    • 我使用“Toast”来确保我的通话时长和费用有价值。但我不知道为什么我不能将文本设置为我的布局。我尝试使用像 textView.setText("abcd"); 这样的原始字符串进行设置但是还是不行
    猜你喜欢
    • 2014-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多