如何显示此自定义视图?



我在显示此视图时遇到问题。当我转到应该有视图的屏幕时,视图中没有任何反应 以下是相关文件:

创建计时器活动.java

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_timer);
    final Context context = this;
    mVisible = true;
    mControlsView = findViewById(R.id.create_timer_content_controls);
    mContentView = findViewById(R.id.create_timer_content);

    // Set up the user interaction to manually show or hide the system UI.
    mContentView.setOnClickListener((View view) -> {
        toggle();
    });
    /*XmlPullParser parser = getResources().getXml(R.id.view_tim);
    AttributeSet attributes = Xml.asAttributeSet(parser);*/
    final TableLayout tableLayout = (TableLayout) findViewById(R.id.create_timer_table);
    TimerFormView timerFormView = new TimerFormView(context, tableLayout);
    timerFormView.initializeComponents();
}

计时器窗体视图.java

public class TimerFormView extends TableLayout {
    private Context context;
    private final ContentValues timerValues;
    private final ContentValues timerSegmentValues;
    private final TableLayout tableLayout;
    public TimerFormView(Context context, TableLayout tableLayout) {
        this(context, null, tableLayout);
    }
    public TimerFormView(Context context, AttributeSet attrs, TableLayout tableLayout) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.TimerFormView, 0, 0);
        String titleText = "Hello";
        @SuppressWarnings("ResourceAsColor")
        int valueColor = a.getColor(0,
                android.R.color.holo_blue_light);
        a.recycle();
        this.context = context;
        this.tableLayout = tableLayout;
        TimerDatabase timerDatabase = new TimerDatabase(context);
        SQLiteDatabase databaseHelper = timerDatabase.getWritableDatabase();
        timerValues = new ContentValues();
        timerValues.put(TimerDatabase.TimerEntry.COLUMN_NAME_TIMER_NAME, "");
        timerSegmentValues = new ContentValues();
    }
    public TimerFormView(Context context, AttributeSet attrs)
    {
        this(context, attrs, null);
    }
    public void initializeComponents() {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_timer_form, this, true);
        final TextView timerNameTextView = (TextView) findViewById(R.id.timerNameTxt);
        timerNameTextView.setSingleLine(true);
        InputFilter[] filterArrayTimerName = new InputFilter[1];
        filterArrayTimerName[0] = new InputFilter.LengthFilter(32);
        timerNameTextView.setFilters(filterArrayTimerName);
        final Button saveTimerBtn = (Button) findViewById(R.id.saveTimerBtn);
        saveTimerBtn.setEnabled(false);
        saveTimerBtn.setOnClickListener((View v) -> {
            timerValues.put(TimerDatabase.TimerEntry.COLUMN_NAME_TIMER_NAME, timerNameTextView.getText().toString());
        });
        // Enable the timer to be saved once text is entered
        TextWatcher timerNameTextWatcher = new TextWatcher() {
            public void afterTextChanged(Editable s){
                if(s.length() > 0) {
                    saveTimerBtn.setEnabled(true);
                } else {
                    saveTimerBtn.setEnabled(false);
                }
            }
            public void  beforeTextChanged(CharSequence s, int start, int count, int after){
                // you can check for enter key here
            }
            public void  onTextChanged (CharSequence s, int start, int before,int count) {
            }
        };
        EditText timerNameEditText = (EditText) findViewById(R.id.timerNameTxt);
        timerNameEditText.addTextChangedListener(timerNameTextWatcher);

        // Segments
        final TextView segmentNameTextView = (TextView) findViewById(R.id.segmentNameTxt);
        segmentNameTextView.setSingleLine(true);
        InputFilter[] filterArraySegmentName = new InputFilter[1];
        filterArraySegmentName[0] = new InputFilter.LengthFilter(32);
        segmentNameTextView.setFilters(filterArraySegmentName);
        Button addSegmentBtn = (Button) findViewById(R.id.addSegmentBtn);
        addSegmentBtn.setEnabled(false);
        addSegmentBtn.setOnClickListener((View v) -> {
            // Content for the new segment
            TableRow segmentTableRow = new TableRow(context);
            segmentNameTextView.setText(segmentNameTextView.getText());
            segmentTableRow.addView(segmentNameTextView);
            tableLayout.addView(segmentTableRow);
            timerSegmentValues.put(TimerDatabase.TimerSegment.COLUMN_NAME_SEGMENT_NAME, segmentNameTextView.getText().toString());
        });
        TextWatcher segmentNameTextWatcher = new TextWatcher() {
            public void afterTextChanged(Editable s){
                if(s.length() > 0) {
                    saveTimerBtn.setEnabled(true);
                } else {
                    saveTimerBtn.setEnabled(false);
                }
            }
            public void  beforeTextChanged(CharSequence s, int start, int count, int after){
                // you can check for enter key here
            }
            public void  onTextChanged (CharSequence s, int start, int before,int count) {
            }
        };
        EditText segmentNameEditText = (EditText) findViewById(R.id.segmentNameTxt);
        segmentNameEditText.addTextChangedListener(segmentNameTextWatcher);
        EditText segmentDurationEditText = (EditText) findViewById(R.id.segmentDurationTxt);
        //segmentDurationEditText.addTextChangedListener(segmentTextWatcher);
        EditText segmentRepeatEditText = (EditText) findViewById(R.id.segmentRepeatTxt);
        //segmentRepeatEditText.addTextChangedListener(segmentTextWatcher);
        setVisibility(View.VISIBLE);
        super.layout(50, 50, 50, 50);
        setOrientation(LinearLayout.HORIZONTAL);
        setGravity(Gravity.CENTER_VERTICAL);
    }
}

view_timer_form.xml

    <?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="400dp" android:layout_height="400dp">
    <TableRow android:layout_width="400dp" android:layout_height="400dp">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Create Timer"
            android:id="@+id/createTimerLbl" />
    </TableRow>
    <TableRow android:layout_width="match_parent" android:layout_height="wrap_content">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Timer Name"
            android:id="@+id/timerNameLbl"
            android:gravity="top" />
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/timerNameTxt"
            android:layout_column="1" />
    </TableRow>
    <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/addSegmentNameRow">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Segment Name"
            android:id="@+id/segmentNameLbl"
            android:gravity="top" />
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/segmentNameTxt"
            android:layout_column="1" />
    </TableRow>
    <TableRow android:layout_width="match_parent" android:layout_height="wrap_content">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Duration"
            android:id="@+id/segmentDurationLbl"
            android:gravity="top" />
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/segmentDurationTxt"
            android:inputType="number"
            android:layout_column="1" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Seconds"
            android:id="@+id/durationSecondsLbl"
            android:gravity="top" />
    </TableRow>
    <TableRow android:layout_width="match_parent" android:layout_height="wrap_content">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Repeat"
            android:id="@+id/segmentRepeatLbl"
            android:gravity="top" />
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/segmentRepeatTxt"
            android:inputType="number"
            android:layout_column="1" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Times"
            android:id="@+id/segmentRepeatTimesLbl"
            android:gravity="top" />
    </TableRow>
    <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/addSegmentBtnRow">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Segment"
            android:id="@+id/addSegmentBtn" />
    </TableRow>
    <TableRow android:layout_width="match_parent" android:layout_height="wrap_content">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Save Timer"
            android:id="@+id/saveTimerBtn" />
    </TableRow>
</TableLayout>

activity_create_timer.xml

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0099cc"
    tools:context="com.example.timer.CreateTimerActivity">
    <TextView android:id="@+id/create_timer_content" android:layout_width="match_parent"
        android:layout_height="match_parent" android:keepScreenOn="true" android:textColor="#33b5e5"
        android:textStyle="bold" android:textSize="50sp" android:gravity="center"
        android:text="" />
    <!-- This FrameLayout insets its children based on system windows using
         android:fitsSystemWindows. -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
        <LinearLayout
            android:id="@+id/create_timer_content_controls"
            style="?metaButtonBarStyle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp"
            tools:ignore="UselessParent">
            <com.example.timer.views.TimerFormView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/create_timer_table">
            </com.example.timer.views.TimerFormView>
        </LinearLayout>


    </FrameLayout>
</FrameLayout>

我有这样的问题,我以这种方式解决了它......

  • 不要从活动中调用任何自定义视图实例甚至静态变量。
  • 仅定义构造函数(上下文、属性(
  • 并在自定义视图类中初始化您想要的所有内容。

祝你好运

最新更新