Android用户界面设计学习之旅-第六站

不知道大家有没有使用TextView打印文字满屏显示却仍然由于内容太长无法全部显示的困扰,这里提供一个简单的设置方法,供大家借鉴和拍砖^_^
延续上面几站学习的成果,这个实验只需要用到一个java文件以及修改dialog.xml资源配置文件。好了开工……
1、借用以前的Adventure.java做启动界面
package com.penguin.adventure;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Adventure extends Activity {
   private static final String TAG = "Main Activity";

   /** Called when the activity is first created. */
  @Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate ...");
    setContentView(R.layout.main);

     final EditText edt = (EditText) findViewById(R.id.textview_input);

     // try to display a dialog
    Button btnDialog = (Button) findViewById(R.id.button_hi_dialog);
    btnDialog.setOnClickListener( new View.OnClickListener() {

       public void onClick(View v) {
         // TODO Auto-generated method stub
        Intent intent = new Intent();
        intent.putExtra( "str", edt.getText().toString());
        intent.setClass(Adventure. this, AlertDialog. class);
        startActivity(intent);
      }
    });
     // try to call and display a new activity
    Button btnActivity = (Button) findViewById(R.id.button_hi_activity);
    btnActivity.setOnClickListener( new View.OnClickListener() {
      
       public void onClick(View v) {
         // TODO Auto-generated method stub
        Intent intent = new Intent();
        intent.putExtra( "str", edt.getText().toString());
        intent.setAction( "com.penguin.action.Main");
        startActivity(intent);
      }
    });
  }

  @Override
   public void onStart() {
     super.onStart();
    Log.d(TAG, "onStart ...");
  }
  
  @Override
   public void onResume(){
     super.onResume();
    Log.d(TAG, "onResume ...");
  }
  
  @Override
   public void onPause(){
     super.onPause();
    Log.d(TAG, "onPause ...");
  }
  
  @Override
   public void onStop(){
     super.onStop();
    Log.d(TAG, "onStop ...");
  }
  
  @Override
   public void onDestroy(){
     super.onDestroy();
    Log.d(TAG, "onDestroy ...");
  }
  
  @Override
   public void onRestart(){
     super.onRestart();
    Log.d(TAG, "onRestart ...");
  }
  
}
2、修改AlertActivity.java使能界面(屏幕)滚动
补充声明两个问题:经过我重新试验,下面说明(注释起来)的两个语句并非实现视图滚动必须。实现界面滚屏的关键因素 在于修改dialog.xml资源配置文件,把要显示文本界面配置嵌套在 < ScrollView > </ ScrollView > 之间。
package com.penguin.adventure;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;


public class AlertActivity extends Activity {
  @Override
   public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog);
    
    Intent intent = getIntent();
    String str = (String) intent.getExtras().get( "str");
    
    TextView txv = (TextView) findViewById(R.id.textview_hi);
   
    //txv.setEllipsize(TextUtils.TruncateAt.MARQUEE);
    //txv.setMarqueeRepeatLimit(-1);
    
     if( null != str){
      String hi = getResources().getString(R.string.hi);
      txv.setText(hi + "\n" + str + "\n");
       for ( int i = 0; i < 100; i++){
      txv.append( "This is the " + String.valueOf(i) + " times." + "\n");
      }
    }
  }
}

下面两句实现屏幕滚动有待细研究。


txv.setEllipsize(TextUtils.TruncateAt.MARQUEE);
官方说明:If set, causes words that are longer than the view is wide to be ellipsized instead of broken in the middle.
txv.setMarqueeRepeatLimit(-1);
官方说明:Sets how many times to repeat the marquee animation.Only applied if the TextView has marquee enabled. Set to -1 to repeat indefinitely.
这两个函数貌似与html语言有些瓜葛,大概的意思是前一个开启文本滚屏使得显示超出屏幕以外的内容,第二个函数意思是设置滚动次数,-1表示无限次滚动。(有待有识之士斧正)
Android的官方说明文档之简练,有时有点让人崩溃,可能意在让大家动手实验。

3、修改dialog.xml资源配置文件使能滚动设置
<? xml version ="1.0" encoding ="utf-8" ?>
< ScrollView xmlns:android ="http://schemas.android.com/apk/res/android"
         android:layout_width ="fill_parent"
         android:layout_height ="fill_parent"
         >
< TextView
     android:layout_width ="fill_parent"
     android:layout_height ="fill_parent"
     android:id ="@+id/textview_hi"
     android:text ="@string/hi"
     />
</ ScrollView >    
4、补上之前的main.xml主界面布局文件
<? xml version ="1.0" encoding ="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
         android:orientation ="vertical"
         android:layout_width ="fill_parent"
         android:layout_height ="fill_parent"  
         >
    
< EditText
   android:id ="@+id/textview_input"
   android:layout_width ="fill_parent"
   android:layout_height ="wrap_content"
   android:textSize ="20px"
   android:textStyle ="bold"
   />

< Button
   android:id ="@+id/button_hi_dialog"
   android:layout_width ="fill_parent"
   android:layout_height ="wrap_content"
   android:text ="@string/sayhi_dialog"
   />
  
< Button
   android:id ="@+id/button_hi_activity"
   android:layout_width ="fill_parent"
   android:layout_height ="wrap_content"
   android:text ="@string/sayhi_activity"
   />
  
</ LinearLayout >

好了启动你的机器人吧,开始滚屏了,呵呵。




你可能感兴趣的:(UI,android,移动开发,textview,Scollable)