tv1.qt settextt为什么要加空格

11:40 提问
让android的TextView可以滚动
我想要在一个文本视图里显示一段文本,但是文字太多,一屏显示不下。我需要让我的TextView可以滚动。我应该怎么做,这是我的代码。
final TextView tv = new TextView(this);
tv.setBackgroundResource(R.drawable.splash);
tv.setTypeface(face);
tv.setTextSize(18);
tv.setTextColor(R.color.BROWN);
tv.setGravity(Gravity.CENTER_VERTICAL| Gravity.CENTER_HORIZONTAL);
tv.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent e)
Random r = new Random();
int i = r.nextInt(101);
if (e.getAction() == e.ACTION_DOWN)
tv.setText(tips[i]);
tv.setBackgroundResource(R.drawable.inner);
setContentView(tv);
按赞数排序
事实上你不需要使用ScrollView
只要在你的布局的xml文件中设置你的TextView的属性:
android:maxLines = "AN_INTEGER"
android:scrollbars = "vertical"
然后在你的代码中用:
yourTextView.setMovementMethod(new ScrollingMovementMethod())
它可以自由的滚动了。
这就是我完全用XML实现的
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"&
&ScrollView
android:id="@+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true"&
android:id="@+id/TEXT_STATUS_ID"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"/&
&/ScrollView&
&/LinearLayout&
1)android:fillViewport="true"和android:layout_weight="1.0"将使得文本视图占据所有可用的空间。
2)当定义Scrollview,不要指定android:layout_height="fill_parent" 否则scrollview 不起作用!(就因为这个浪费了我一个小时的时间)
我赞成的观点:
为了能够在追加文本之后自动滚动到底部,用这个:
mTextStatus = (TextView) findViewById(R.id.TEXT_STATUS_ID);
mScrollView = (ScrollView) findViewById(R.id.SCROLLER_ID);
private void scrollToBottom()
mScrollView.post(new Runnable()
public void run()
mScrollView.smoothScrollTo(0, mTextStatus.getBottom());
textView.post(new Runnable() {
public void run() {
// TODO Auto-generated method stub
textView.append(line);
final int scrollAmount = textView.getLayout().getLineTop(textView.getLineCount()) - textView.getHeight();
if (scrollAmount & 0)
textView.scrollTo(0, scrollAmount);
textView.scrollTo(0, 0);
所有的方法中真的有用的是setMovementMethod()。这是用LinearLayout的一个示例代码。
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello"
&/LinearLayout&
WordExtractTest.java
public class WordExtractTest extends Activity {
TextView tv1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView)findViewById(R.id.tv1);
loadDoc();
private void loadDoc(){
String s = "";
for(int x=0;x&=100;x++){
s += "Line: "+String.valueOf(x)+"\n";
tv1.setMovementMethod(new ScrollingMovementMethod());
tv1.setText(s);
“niangzhi ”这个用户回答的能实现,但没有具体解释清楚,好像导致了很多人的困惑。现在将解决方法,再详细说一下。**yourTextView.setMovementMethod(new ScrollingMovementMethod()), 这个才是正在让textView实现滚动的方法。**
android:maxLines = "AN_INTEGER"
这个属性是设置文本显示地最大行数;
android:scrollbars = "vertical"
这个属性是控制滚动条是否显示。
这样做过后怎么去掉右边的滑动条?
“niangzhi ”这个用户回答的能实现,但没有具体解释清楚,好像导致了很多人的困惑。现在将解决方法,再详细说一下。
yourTextView.setMovementMethod(new ScrollingMovementMethod())
, 这个才是正在让textView实现滚动的方法。
android:maxLines = "AN_INTEGER"
这个属性是设置文本显示地最大行数;
android:scrollbars = "vertical"
这个属性是控制滚动条是否显示。
其他相关推荐第一个小应用:图片浏览器 之 三
学会使用内部存储 - qq_的博客 - CSDN博客
第一个小应用:图片浏览器 之 三
学会使用内部存储
Android开发之自己动手写小应用
什么是内部存储
内部存储就是手机应用里面的数据,对其他应用来说是不可见的。
往内部存储文件中写入数据:
openFileOutput(文件名,操作模式)
返回类型是
FileOutputStream
如果操作的是文本文件,还需要把字节流封装成字符流,需要注意字符编码方式
OutputStreamWriter osw = new OutputStreamWriter (字节流对象,编码方式)
osw.write()
读数据的方式对应的是:
FileInputStream
fir = openFileInput(文件名);
InputStreamReader isr = new InputStreamReader(fir,”UTF-8”);
char[] buffer
= new char[fir.available()]
isr.read(buffer)
把数据读到buffer 中
刷新缓冲区
关闭流对象
后打开的先关闭
界面如下:
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private String fileName = "test";
private EditText etT
private TextView tvS
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etText = (EditText) findViewById(R.id.etText);
tvShow = (TextView) findViewById(R.id.tvShow);
findViewById(R.id.btnWrite).setOnClickListener(this);
findViewById(R.id.btnRead).setOnClickListener(this);
public void onClick(View v) {
switch (v.getId()){
case R.id.btnWrite:
FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
osw.write(etText.getText().toString());
Toast.makeText(getApplicationContext(),"写入完毕",Toast.LENGTH_SHORT).show();
osw.flush();
fos.flush();
osw.close();
fos.close();
etText.setText("");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
case R.id.btnRead:
FileInputStream fis = openFileInput(fileName);
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
char[] buffer = new char[fis.available()];
isr.read(buffer);
isr.close();
fis.close();
String read = new String(buffer);
tvShow.setText(read);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
主布局代码:
&?xml version="1.0" encoding="utf-8"?&
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.wang.readandwriteinsidedata.MainActivity"
android:weightSum="1"&
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入内容:"
android:id="@+id/etText"/&
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="往内部存储文件写入数据"
android:id="@+id/btnWrite"/&
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取内部存储文件数据"
android:id="@+id/tvShow"
android:layout_weight="0.60"/&
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取内部存储文件数据"
android:id="@+id/btnRead"/&
我的热门文章提取用户输入到EditText中的文本信息并暂时储存。
getText()方法用于检索数据,String变量来存储数据,setText()方法在应用别处显示数据。
字符串变量
strings.xml包含整个应用程序中使用的字符串文件,字符串也可以在活动中创建来储存文本值。变量是指在设备的内存中创建一个容器,以容纳各种类型的数据。字符串是一种数据类型,由字母,数字,符号和空格组成。
在活动中创建字符串:
1、声明变量使之可以使用。(声明一个名为strName的变量)
String strN
2、实例化变量,放置字符串数据在strName中。
getText()方法
在活动创建一个EditText对象以从EditText中查看检索数据,并使用findViewById()方法从布局找到EditText。例如:
EditText etName = (EditText) findViewById(R.id.editText1)
后可使用getText()方法来检索与该视图关联的文本信息,它需要被转换为字符串。例如:
strName = etName.getText().toString()
此代码将检索从EditText上查看的文本和存储在变量的信息strName。
setText()方法
使用getText()方法,必须先创建一个TextView对象,并使用该findViewById()方法来从布局中获取信息。例如:
TextView tvName = (TextView) findViewById(R.id.textView1)
然后,更改tvName的文本,可以使用
tvName.setText(strName)
这样TextView中就可以显示任何strName中储存的任何值。
除此之外,使用setText()可以使用+将字符串和文本结合在一起。hard coded text需要加引号,字符串变量不需要。例如:
tvName.setText("Hello " + strName + "'s World!")
TextView的文本会读出Hello Willis’s World!。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1207次
排名:千里之外
(window.slotbydup = window.slotbydup || []).push({
id: '4740887',
container: s,
size: '250,250',
display: 'inlay-fix'tv.setText(test);
也不报错,百度查了半天,也没结果,请教了
public class MyFragment5 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_my_fragment5, container, false);
TextView tv = (TextView) view.findViewById(R.id.text00);
String test = (String) getArguments().get("name1")+"";
tv.setText(test);
Toast.makeText(getActivity(), "已经成功接收" + test, Toast.LENGTH_SHORT).show();
return inflater.inflate(R.layout.fragment_my_fragment5, container, false);
写下你的评论...
写下你的评论...
写下你的评论...
Copyright (C)
All Rights Reserved | 京ICP备 号-2}

我要回帖

更多关于 安卓settext 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信