Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何才能显示此自定义视图?_Java_Android_View_Android Tablelayout - Fatal编程技术网

Java 如何才能显示此自定义视图?

Java 如何才能显示此自定义视图?,java,android,view,android-tablelayout,Java,Android,View,Android Tablelayout,我在获取此视图以显示时遇到问题。当我转到应该有视图的屏幕时,视图中没有任何事情发生,以下是相关文件: CreateTimerActivity.java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_create_timer); final Con

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

CreateTimerActivity.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();
}
TimerFormView.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);
    }
}
查看\u timer\u 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>

活动\u创建\u 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>

我有这样的问题,我用这种方法解决了它

  • 不要从活动中调用任何customView实例,甚至不要调用静态变量
  • 仅使用(上下文、属性)定义构造函数
  • 并初始化customView类中所需的所有内容

祝您好运

在自定义视图之前添加程序包名称class@EliasFazel我只是换了一个真实的例子…从活动中删除对customView的任何调用,也删除第一个构造函数(只设置带有上下文和属性的构造函数),并在视图中初始化您想要的内容class@EliasFazel谢谢你,真管用!所以我会写下来作为答案,请接受。