Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.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
在android中显示多行文本的控件_Android_Android Controls - Fatal编程技术网

在android中显示多行文本的控件

在android中显示多行文本的控件,android,android-controls,Android,Android Controls,我想在我的应用程序中添加一个控件。该控件的特点是它包含多行文本(例如5行),并且不包含滚动条。我将通过下面的例子进行解释 |-----------------| | text1 | | text2 | | text3 | | text4 | | | |-----------------| 如果将字符串添加到文本视图,它将成为 |-----------------| | text1

我想在我的应用程序中添加一个控件。该控件的特点是它包含多行文本(例如5行),并且不包含滚动条。我将通过下面的例子进行解释

|-----------------|
| text1           |
| text2           |
| text3           |
| text4           |
|                 |
|-----------------|
如果将字符串添加到文本视图,它将成为

|-----------------|
| text1           |
| text2           |
| text3           |
| text4           |
| text5           |
|-----------------|
如果您添加另一行文本,它将成为

|-----------------|
| text2           |
| text3           |
| text4           |
| text5           |
| text6           |
|-----------------|

可能吗?我在谷歌上搜索过,但找不到解决方案。

如果你想让它成为一个可重用的控件,你需要为此编写一个自定义视图,你可以在其中添加文本,并在内部文本视图之间切换文本

或者,当您想要添加新行时,您可以使用多个文本视图并将文本从一个视图复制到另一个视图


该功能未作为标准配置包含。

如果希望它是一个可重用的控件,则需要为此编写一个自定义视图,您可以向其中添加文本,并在内部文本视图之间切换文本

或者,当您想要添加新行时,您可以使用多个文本视图并将文本从一个视图复制到另一个视图


该功能未作为标准配置包含。

如果希望它是一个可重用的控件,则需要为此编写一个自定义视图,您可以向其中添加文本,并在内部文本视图之间切换文本

或者,当您想要添加新行时,您可以使用多个文本视图并将文本从一个视图复制到另一个视图


该功能未作为标准配置包含。

如果希望它是一个可重用的控件,则需要为此编写一个自定义视图,您可以向其中添加文本,并在内部文本视图之间切换文本

或者,当您想要添加新行时,您可以使用多个文本视图并将文本从一个视图复制到另一个视图


该功能未作为标准提供。

我不知道为什么您可能希望在没有滚动条的情况下使用多行,但如果您需要滚动条,我认为这可能是一个可行的解决方案

显然,你需要在你的布局上做一些逻辑,因为我想不出一个视图能给你这个服务而不做一些调整

所以,我想用5个文本视图(文本视图的数量取决于你想显示的行数)来创建一个RelativeLayout

因此,每个文本视图将包含一行内容,因此当用户添加新行时,您必须按照以下内容进行操作:

if( linesInView == 5 )
    shiftText();

addText();
因此,
shiftText()
将负责遍历一个循环,并将一个文本视图的文本设置为另一个文本视图(移动它们),以便为新文本留出空间

然后,
addText()
只需将用户创建的新文本添加到由
shiftText()
生成的释放点

总之,你可以遵循这些准则。请记住,我只是在头脑中编写了这段代码,目前我没有办法检查它的正确性。此外,这是第一次尝试,还有改进的余地:

private static int MAX_NUM_LINES = 5;
private int linesShown = 0;
private ArrayList<TextView> views = new ArrayList<>();
private EditText userInput;

public void onCreate(...){
   ....
   views.add((TextView)findViewById(R.id.firstline);
   views.add((TextView)findViewById(R.id.secondline);
   views.add((TextView)findViewById(R.id.thirdline);
   views.add((TextView)findViewById(R.id.fourthline);
   views.add((TextView)findViewById(R.id.fifthline);

   this.userInput = (EditText) findViewById(R.id.input);
  //I suggest these in order to avoid calling "findViewById()" everytime the user 
 // adds a new sentences, which I gather will happen often
}

public void someEventCalledWhenUserAddsNewLine(View view){
    if(this.linesShown>=MAX_NUM_LINES)
          shiftText();

    addNewLine();        
}

   private void shiftText(){
       //Shift your text by looping through your this.views
       for(pos=0; pos<this.views.size()-1; ++pos){
           String nextLine = this.views.get(pos+1).getText();
           this.views.get(pos).setText(nextLine);
       }
       //You need to set the condition to this.views.size()-1 because
      //when you reach position 4 (your last TextView) you have to sop
      // since there is not text to shift to that one
      this.views.get(this.views.size()-1).setText(null);
      //Make sure you set the text of the last TextView to null so that
      // addLine() will be able to find the first available TextView
   }

   private void addNewLine(){
      //Add text from this.userInput to the first available TextView in this.views
      int pos;
      for(pos=0; pos<this.views.size(); ++pos){
           if(this.views.get(pos).getText()==null){ 
              //We have found the first available TextView
                   break;
           }
      }  
      this.views.get(pos).setText(this.userInput.getText()); 
      //Pos holds the position of the first available TextView, now we have
      // added the new line  
     this.linesShown++;         
   }
private static int MAX_NUM_line=5;
专用int linesShown=0;
私有ArrayList视图=新建ArrayList();
私有编辑文本用户输入;
创建时的公共无效(…){
....
添加((TextView)findViewById(R.id.firstline);
添加((TextView)findViewById(R.id.secondline);
添加((文本视图)findViewById(R.id.thirdline);
添加((文本视图)findViewById(R.id.fourthline);
添加((文本视图)findViewById(R.id.fifthline);
this.userInput=(EditText)findViewById(R.id.input);
//为了避免每次用户调用“findviewbyd()”,我建议使用这些方法
//添加了一个新的句子,我收集到这将经常发生
}
public void someevent调用henuseraddsnewline(视图){
如果(this.linesShown>=最大行数)
shiftText();
addNewLine();
}
私有void shiftText(){
//通过在this.views中循环移动文本

对于(pos=0;pos我不知道为什么您可能希望有多行而没有滚动条,但如果这是您需要的,我认为这可能是一个可能的解决方案

显然,你需要在你的布局上做一些逻辑,因为我想不出一个视图能给你这个服务而不做一些调整

所以,我想用5个文本视图(文本视图的数量取决于你想显示的行数)来创建一个RelativeLayout

因此,每个文本视图将包含一行内容,因此当用户添加新行时,您必须按照以下内容进行操作:

if( linesInView == 5 )
    shiftText();

addText();
因此,
shiftText()
将负责遍历一个循环,并将一个文本视图的文本设置为另一个文本视图(移动它们),以便为新文本留出空间

然后,
addText()
只需将用户创建的新文本添加到由
shiftText()
生成的释放点

总而言之,您可以遵循这些指导原则。请记住,我只是凭空编写了这段代码,目前我没有办法检查它的正确性。此外,这是第一次尝试,还有改进的余地:

private static int MAX_NUM_LINES = 5;
private int linesShown = 0;
private ArrayList<TextView> views = new ArrayList<>();
private EditText userInput;

public void onCreate(...){
   ....
   views.add((TextView)findViewById(R.id.firstline);
   views.add((TextView)findViewById(R.id.secondline);
   views.add((TextView)findViewById(R.id.thirdline);
   views.add((TextView)findViewById(R.id.fourthline);
   views.add((TextView)findViewById(R.id.fifthline);

   this.userInput = (EditText) findViewById(R.id.input);
  //I suggest these in order to avoid calling "findViewById()" everytime the user 
 // adds a new sentences, which I gather will happen often
}

public void someEventCalledWhenUserAddsNewLine(View view){
    if(this.linesShown>=MAX_NUM_LINES)
          shiftText();

    addNewLine();        
}

   private void shiftText(){
       //Shift your text by looping through your this.views
       for(pos=0; pos<this.views.size()-1; ++pos){
           String nextLine = this.views.get(pos+1).getText();
           this.views.get(pos).setText(nextLine);
       }
       //You need to set the condition to this.views.size()-1 because
      //when you reach position 4 (your last TextView) you have to sop
      // since there is not text to shift to that one
      this.views.get(this.views.size()-1).setText(null);
      //Make sure you set the text of the last TextView to null so that
      // addLine() will be able to find the first available TextView
   }

   private void addNewLine(){
      //Add text from this.userInput to the first available TextView in this.views
      int pos;
      for(pos=0; pos<this.views.size(); ++pos){
           if(this.views.get(pos).getText()==null){ 
              //We have found the first available TextView
                   break;
           }
      }  
      this.views.get(pos).setText(this.userInput.getText()); 
      //Pos holds the position of the first available TextView, now we have
      // added the new line  
     this.linesShown++;         
   }
private static int MAX_NUM_line=5;
专用int linesShown=0;
私有ArrayList视图=新建ArrayList();
私有编辑文本用户输入;
创建时的公共无效(…){
....
添加((TextView)findViewById(R.id.firstline);
添加((TextView)findViewById(R.id.secondline);
添加((文本视图)findViewById(R.id.thirdline);
添加((文本视图)findViewById(R.id.fourthline);
添加((文本视图)findViewById(R.id.fifthline);
this.userInput=(EditText)findViewById(R.id.input);
//为了避免每次用户调用“findviewbyd()”,我建议使用这些方法
//添加了一个新的句子,我收集到这将经常发生
}
public void someevent调用henuseraddsnewline(视图){
如果(this.linesShown>=最大行数)
shiftText();
addNewLine();
}
私有void shiftText(){
//转移你的注意力