Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.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_Android Layout - Fatal编程技术网

Java 创建自定义视图并重用同一视图

Java 创建自定义视图并重用同一视图,java,android,android-layout,Java,Android,Android Layout,我有一个由五行表组成的表布局。每个表行都有一个自定义视图,用于呈现一些图形和自定义文本视图。我的布局如下所示 <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">

我有一个由五行表组成的表布局。每个表行都有一个自定义视图,用于呈现一些图形和自定义文本视图。我的布局如下所示

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <com.company.views.FirstView
        android:id="@+id/firstView"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight=".80"
        android:background="#000000" />
    <com.company.views.FirstTextView
        android:id="@+id/firstTextView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".20"
        android:background="#000000"
        android:textColor="#00FF00"
        android:gravity="center"
        android:textStyle="bold|italic"
        android:textSize="40sp"
        android:inputType="number"
        android:digits="1234567890"
        android:cursorVisible="false"
        android:scrollbars = "vertical"/>
</TableRow>
<TableRow
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
    <com.company.views.SecondView
        android:id="@+id/secondView"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight=".80"
        android:background="#000000" />
    <com.company.views.SecondTextView
        android:id="@+id/secondTextView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".20"
        android:background="#000000"
        android:textColor="#FFFF00"
        android:gravity="center"
        android:textStyle="bold|italic"
        android:textSize="40sp"
        android:inputType="number"
        android:digits="1234567890"
        android:cursorVisible="false"
        android:scrollbars = "vertical"/>
</TableRow>
</TableLayout>
private void init(AttributeSet attrs, int defStyle) {
    textView = (TextView)findViewById(R.id.firstTextView);
    textView.setText("My Awesome Text");;
}
public class FirstTextView extends AppCompatTextView {
private int spinnerText=120;
private float oldY=0;
private TextView textView;
private Context context;
private PopupWindow mPopupWindow;

public FirstTextView(Context context) {
    super(context);
    this.context = context;
    init(null, 0);
}

public FirstTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    init(attrs, 0);

}

public FirstTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.context = context;
    init(attrs, defStyle);
}

private void init(AttributeSet attrs, int defStyle) {
    //cant do that as the findviewbyid return null
    textView = (TextView)findViewById(R.id.firstView);
    textView.setOnTouchListener(handleTouch);
    // Here i want to find which textview firstView or SecondView. 
    //if(firstview)
       //textView.setText(""+Physiology.getInstance().Model1.getValue());
    //else 
      //textView.setText(""+Physiology.getInstance().Model2.getValue());  
     textView.setText(""+Physiology.getInstance().Model2.getValue());
}

@Override
public void setText(CharSequence text, BufferType type) {
    String newText="";
    if(text.length()>0){
        newText=String.valueOf(text);
        int textasNumber=Integer.valueOf(newText);
        Physiology.getInstance().mBloodPressure.setSystolic(textasNumber);
        newText= Physiology.getInstance().mBloodPressure.getSystolic() + "/" +  (int)Physiology.getInstance().mBloodPressure.mDiastolic;
    }

    super.setText(newText,type);
}

public void onTextViewClick(View v) {
    // Initialize a new instance of LayoutInflater service
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);

    // Inflate the custom layout/view
    NumberPadView customView = (NumberPadView) inflater.inflate(R.layout.number_popup,null);

    // Initialize a new instance of popup window
    mPopupWindow = new PopupWindow(
            customView,
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
    );

    customView.init(Physiology.getInstance().mBloodPressure.getSystolic(),mPopupWindow,this);

    // Finally, show the popup window at the center location of root
    mPopupWindow.showAtLocation(v, Gravity.CENTER,0,0);
}
  }
代码因充气异常而中断。我被困在这里,因为我不知道我做错了什么。有没有其他方法可以达到预期的结果。还是我遗漏了一些明显的东西

我的FirstTextView如下所示

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <com.company.views.FirstView
        android:id="@+id/firstView"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight=".80"
        android:background="#000000" />
    <com.company.views.FirstTextView
        android:id="@+id/firstTextView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".20"
        android:background="#000000"
        android:textColor="#00FF00"
        android:gravity="center"
        android:textStyle="bold|italic"
        android:textSize="40sp"
        android:inputType="number"
        android:digits="1234567890"
        android:cursorVisible="false"
        android:scrollbars = "vertical"/>
</TableRow>
<TableRow
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
    <com.company.views.SecondView
        android:id="@+id/secondView"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight=".80"
        android:background="#000000" />
    <com.company.views.SecondTextView
        android:id="@+id/secondTextView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".20"
        android:background="#000000"
        android:textColor="#FFFF00"
        android:gravity="center"
        android:textStyle="bold|italic"
        android:textSize="40sp"
        android:inputType="number"
        android:digits="1234567890"
        android:cursorVisible="false"
        android:scrollbars = "vertical"/>
</TableRow>
</TableLayout>
private void init(AttributeSet attrs, int defStyle) {
    textView = (TextView)findViewById(R.id.firstTextView);
    textView.setText("My Awesome Text");;
}
public class FirstTextView extends AppCompatTextView {
private int spinnerText=120;
private float oldY=0;
private TextView textView;
private Context context;
private PopupWindow mPopupWindow;

public FirstTextView(Context context) {
    super(context);
    this.context = context;
    init(null, 0);
}

public FirstTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    init(attrs, 0);

}

public FirstTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.context = context;
    init(attrs, defStyle);
}

private void init(AttributeSet attrs, int defStyle) {
    //cant do that as the findviewbyid return null
    textView = (TextView)findViewById(R.id.firstView);
    textView.setOnTouchListener(handleTouch);
    // Here i want to find which textview firstView or SecondView. 
    //if(firstview)
       //textView.setText(""+Physiology.getInstance().Model1.getValue());
    //else 
      //textView.setText(""+Physiology.getInstance().Model2.getValue());  
     textView.setText(""+Physiology.getInstance().Model2.getValue());
}

@Override
public void setText(CharSequence text, BufferType type) {
    String newText="";
    if(text.length()>0){
        newText=String.valueOf(text);
        int textasNumber=Integer.valueOf(newText);
        Physiology.getInstance().mBloodPressure.setSystolic(textasNumber);
        newText= Physiology.getInstance().mBloodPressure.getSystolic() + "/" +  (int)Physiology.getInstance().mBloodPressure.mDiastolic;
    }

    super.setText(newText,type);
}

public void onTextViewClick(View v) {
    // Initialize a new instance of LayoutInflater service
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);

    // Inflate the custom layout/view
    NumberPadView customView = (NumberPadView) inflater.inflate(R.layout.number_popup,null);

    // Initialize a new instance of popup window
    mPopupWindow = new PopupWindow(
            customView,
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
    );

    customView.init(Physiology.getInstance().mBloodPressure.getSystolic(),mPopupWindow,this);

    // Finally, show the popup window at the center location of root
    mPopupWindow.showAtLocation(v, Gravity.CENTER,0,0);
}
  }

由于自定义视图扩展了TextView,因此不需要执行findViewById。您的对象本身已经是视图

因此,您只需在init()中调用setText(“甚至是awesomer”)

您的代码会中断,因为findViewById返回null,然后您会得到一个NPE试图对其调用setText()

要区分视图中的视图,只需使用getId()

但是从你的问题来看,我认为你只是想把你的自定义视图和其他视图一样对待。在这种情况下,执行对其他视图执行的操作。在活动的onCreate()中,在setContentView()之后执行以下操作:


由于自定义视图扩展了TextView,因此不需要执行findViewById。您的对象本身已经是视图

因此,您只需在init()中调用setText(“甚至是awesomer”)

您的代码会中断,因为findViewById返回null,然后您会得到一个NPE试图对其调用setText()

要区分视图中的视图,只需使用getId()

但是从你的问题来看,我认为你只是想把你的自定义视图和其他视图一样对待。在这种情况下,执行对其他视图执行的操作。在活动的onCreate()中,在setContentView()之后执行以下操作:

如果您只想更改文本,我会直接将文本放入XML文件中:

<com.company.views.FirstTextView
    android:id="@+id/firstTextView"
    android:text="my first text
    .../>
总的来说,我认为这不是一个好主意,因为您的视图(通常)应该在不知道自己的ID是什么的情况下工作。。。但如果您需要,您仍然可以这样做。

如果您只想更改文本,我会直接将文本放入XML文件中:

<com.company.views.FirstTextView
    android:id="@+id/firstTextView"
    android:text="my first text
    .../>


总的来说,我认为这不是一个好主意,因为您的视图(通常)应该在不知道自己的ID是什么的情况下工作。。。但是如果您需要的话,您仍然可以这样做。

我们不知道
FirstTextView
代码是什么,我想它扩展了
TextView
是的,FirstTextView和secondTextView都扩展了TextView我认为需要更多的代码,用于上下文。是否需要
布局宽度
?我以为是,将stacktrace表单放入logcat。是的,布局宽度是必需的,并且也存在于我的代码中。我只是没有将所有代码复制到此帖子,因为帖子可能会变得臃肿。问题是您想为
firstTextView
secondTextView
设置两个不同的文本值吗?我们不知道
FirstTextView
code是,我想它扩展了
TextView
yes FirstTextView和secondTextView都扩展了TextView。我认为需要更多的代码,用于上下文。是否需要
布局宽度
?我以为是,将stacktrace表单放入logcat。是的,布局宽度是必需的,并且也存在于我的代码中。我只是没有将所有代码复制到此帖子中,因为帖子可能会变得臃肿。问题是您希望对
firstTextView
secondTextView
使用两个不同的文本值吗?但是我想通过id查找视图,这样我就知道了设置哪个文本。但是调用
init()
的位置在哪里,我认为它是在
活动中的,如果它来自视图类,当然是错误的,每个init都是从每个java视图类的构造函数调用的。那么,您是在调用
findViewById(R.id.firstTextView)
在您的自定义
TextView
类中?是的,它是从自定义TextView类中调用的,但我不知道我所做的是否正确,因为我是android的新手。但是我想通过id查找视图,这样我就知道要设置哪个文本。但是调用
init()
的位置在哪里,我想它是在
活动中的,如果它来自视图类,那当然是错误的,每个init都是从每个java视图类的构造函数调用的
在您的自定义
TextView
类中?是的,它是从自定义TextView类中调用的,但我不知道我所做的是否正确,因为我是android的新手。感谢您的回答,但是如果我想在用户单击TextView时继续更改TextView的文本,我知道我可以添加一个单击事件,然后更改文本,该怎么办但是我想区分单击哪个视图来查找我要设置的文本。我想你必须提供更多的原始代码,以便我们可以看到区别。我已经更新了问题,因此它有我的原始代码。我将尝试看看getid()是否有效,因为如果我能获得id,那么我认为我已经解决了我的问题。谢谢你的帮助。你的第二次编辑让我找到了正确的方向。谢谢你的回答,但是如果我想在用户单击textview时继续更改textview的文本该怎么办?我知道我可以添加一个单击事件,然后更改文本,但我想区分单击哪个视图来查找我要设置的文本。我想你必须提供更多的信息您的原始代码,以便我们可以看到差异是什么我已经更新了问题,因此它有我的原始代码。我将尝试看看getid()是否有效,因为如果我能获得id,那么我认为我已经解决了我的问题。谢谢你的帮助。你的第二次编辑让我找到了正确的方向。