在 Android 选框中动态将文本设置为文本视图



如何将 TextView Text 设置为动态滚动选框。请帮助我,谢谢提前。

用于获取要在音频播放器中播放的列表项的列表视图如下所示。

    public class Audiovediolist extends Activity implements OnItemClickListener {
        private ListView audioList;
        private Intent frmHome;
        private ArrayList<HashMap<String, String>> dataList;
        private HashMap<String, String> map;
        private HashMap<String, String> data;
        private int Screen_Id;
        String name;
        String mSign[] ={"1","2","3","4","5"}; 
        Integer mImages[] = { R.drawable.m, R.drawable.v,
                R.drawable.mm, R.drawable.k, R.drawable.s};
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_RIGHT_ICON);
            setContentView(R.layout.list);
            frmHome = getIntent();
            getIntentValues();
            initUI();
            setUI();
            uiListener();
        }
        private void initUI() {
                audioList = (ListView) findViewById(R.id.audiolist_listView);
                dataList = new ArrayList<HashMap<String, String>>();
        }
        private void uiListener() {
                audioList.setOnItemClickListener(this);
        }
        private void getIntentValues() {
            String id = frmHome.getStringExtra("Activity_Id");
            Screen_Id = Integer.parseInt(id);
            if (Screen_Id == 1) {
                getWindow().setFeatureDrawableResource(Window.FEATURE_RIGHT_ICON,
                        R.drawable.audioo);
                this.setTitle(getResources().getString(R.string.audio));
                this.setTitleColor(getResources().getColor(R.color.Beige));
            } else {
                this.setTitle(getResources().getString(R.string.video));
                this.setTitleColor(getResources().getColor(R.color.Beige));
                getWindow().setFeatureDrawableResource(Window.FEATURE_RIGHT_ICON,
                        R.drawable.videoo);
                 }
          }
        private void setAdapter() {
            SimpleAdapter adapter = new SimpleAdapter(this, dataList,
                                     R.layout.list_row, new String[] { "Image", "text" },
                                     new int[] {
                                     R.id.audiolist_row_img, R.id.audiolist_row_tv });
                         audioList.setAdapter(adapter);
        }
        private void setUI() {
                for (int a = 0; a <= 11; a++) {
                     map = new HashMap<String, String>();
                     map.put("Image", "" + mImages[a]);
                     map.put("text", mSign[a]);
                     dataList.add(map);
                     map = null;
                }
        }
        @Override
        protected void onStart() {
            super.onStart();
            setAdapter();
        }
        @Override
        public void onBackPressed() {
            super.onBackPressed();
            System.gc();
        }
    /*private class NextTask extends AsyncTask<Void,Void,Void> {
                ProgressDialog progressDailog = new ProgressDialog(Audiovediolist.this);
              @Override
              protected void onPreExecute() {
                super.onPreExecute();
                progressDailog.setTitle("Please Wait");
                progressDailog.setMessage("URL Data Loading ...");
                progressDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                progressDailog.setCancelable(true);
                progressDailog.show();
                }
              @SuppressWarnings("static-access")
            @Override
              protected Void doInBackground(Void... params) {
                return null;
                }
             @Override
             protected void onPostExecute(Void result) {
                progressDailog.dismiss();
                }
    }*/
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
                    data = dataList.get(position);
                try {
                      NetWorkCheck netWork = new NetWorkCheck();
                      if (netWork.isNetWorkConnection(this)) {
                          if (Screen_Id == 1) {
                            //  new NextTask().execute();
                              Intent audioInt=new Intent(getApplicationContext(),AudioView.class);
                                audioInt.setType(data.get("text"));
                                startActivity(audioInt);
                           } else if (Screen_Id == 2) {
                                 Intent vedioInt = new Intent(this, VideoPlayActivity.class);
                                 vedioInt.setType(data.get("text"));
                                 startActivity(vedioInt);
                                 }
                       } else {
                              netWork.alert();
                              }  
                    } catch (Exception e) {
                            e.printStackTrace();
                            Toast.makeText(this, "Problem while ListItemClick", Toast.LENGTH_SHORT).show();
                            }
    }
    }

使用的音频播放器的代码是

public class AudioView extends Activity implements OnClickListener {
    private MediaPlayer mMediaPlayer;
    private ImageView mPlay;
    private ImageView mPause;
    private ImageView mBack;
    private ImageView mFar;
    private ProgressBar myProgressBar;
    // private PlayerService mService;
    private boolean boolFlag = false;
    private boolean isPausedInCall = false;
    private String url;
    private TelephonyManager mTelephoneMgr;
    private NotificationManager mNotificationManager;
    private int SIMPLE_NOTFICATION_ID;
    private TextView tv;
    // private boolean serviceRunning;
    Notification notifyDetails;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.audio_main);
        getIntentValues();
        initUI();
        uiListener();
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notifyDetails = new Notification(R.drawable.mulug_icon, url,
                System.currentTimeMillis());
        mTelephoneMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if (mTelephoneMgr != null) {
            mTelephoneMgr.listen(phoneStateListener,
                    PhoneStateListener.LISTEN_CALL_STATE);
        }
    }
    private void getIntentValues() {
        url = getIntent().getType();

    }
    private void uiListener() {
        mPlay.setOnClickListener(this);
        mPlay.setOnClickListener(this);
        mPause.setOnClickListener(this);
        mPause.setVisibility(android.view.View.INVISIBLE);
        mBack.setOnClickListener(this);
        mFar.setOnClickListener(this);
    }
    private void initUI() {
        tv = (TextView) findViewById(R.id.text);
        mBack = (ImageView) findViewById(R.id.back);
        mPlay = (ImageView) findViewById(R.id.play);
        mPause = (ImageView) findViewById(R.id.pause);
        mFar = (ImageView) findViewById(R.id.fwd);
        myProgressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
        myProgressBar.setProgressDrawable(getResources().getDrawable(
                R.drawable.green_progress));
        myProgressBar.setProgress(0);
            tv.setText(url);
            tv.setFocusable(true);
    }
    private Thread myThread = new Thread() {
        public void run() {
            while (mMediaPlayer.getCurrentPosition() < mMediaPlayer
                    .getDuration()) {
                try {
                    myProgressBar
                            .setProgress(mMediaPlayer.getCurrentPosition());
                } catch (Throwable t) {
                }
            }
        }
    };
    @SuppressLint("NewApi")
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        mMediaPlayer.stop();
        // myThread.stop();
        // mMediaPlayer.release();
        mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
        System.gc();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        System.gc();
    }
    @SuppressLint("NewApi")
    @Override
    protected void onStart() {
        super.onStart();
        this.setProgressBarIndeterminateVisibility(true);
        try {
            if (boolFlag == false) {
                System.out.println(FinalConstants.URL + "mobile-audio/" + url
                        + ".mp3");
                mMediaPlayer = MediaPlayer.create(
                        this,
                        Uri.parse(FinalConstants.URL + "mobile-audio/" + url
                                + ".mp3"));
            }
            myProgressBar.setMax(mMediaPlayer.getDuration());
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Loading Problems",
                    Toast.LENGTH_SHORT).show();
            Log.e("ERRORS", "ServerFailed to Load...");
            super.onBackPressed();
            e.printStackTrace();
        }
    }
    public PhoneStateListener phoneStateListener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            if (state == TelephonyManager.CALL_STATE_RINGING) {
                System.out.println("ringing state");
                if (mMediaPlayer != null) {
                    pauseMedia();
                    isPausedInCall = true;
                }
                notificationShow();
            }
            if (state == TelephonyManager.CALL_STATE_IDLE) {
                System.out.println("callState Idle");
                if (mMediaPlayer != null) {
                    if (isPausedInCall) {
                        isPausedInCall = false;
                        playMedia();
                    }
                }
            }
            if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
            }
            super.onCallStateChanged(state, incomingNumber);
        }
    };
    private void pauseMedia() {
        mMediaPlayer.pause();
        changePlayerIcons(false);
    }
    private void playMedia() {
        mMediaPlayer.start();
        changePlayerIcons(true);
    }
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.play:
            if (!boolFlag) {
                try {
                    notificationShow();
                    mMediaPlayer.setLooping(false);
                    // mMediaPlayer.prepareAsync();
                } catch (Exception e) {
                    Toast.makeText(this, "preparing", Toast.LENGTH_SHORT)
                            .show();
                }
                boolFlag = true;
                mMediaPlayer.start();
                myThread.start();
            } else {
                notificationShow();
                mMediaPlayer.getCurrentPosition();
                mMediaPlayer.start();
            }
            changePlayerIcons(true);
            myProgressBar.setProgress(mMediaPlayer.getCurrentPosition());
            mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer arg0) {
                    changePlayerIcons(false);
                    myProgressBar.setProgress(0);
                    onStart();
                }
            });
            mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                }
            });
            mMediaPlayer.setOnErrorListener(new OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    Toast.makeText(getApplicationContext(),
                            "MediaPlayer Errors", Toast.LENGTH_SHORT).show();
                    Log.e("ERRORS", "MediaPlayer Errors...");
                    return false;
                }
            });
            break;
        case R.id.pause:
            mMediaPlayer.pause();
            mPlay.setVisibility(android.view.View.VISIBLE);
            mPause.setVisibility(android.view.View.INVISIBLE);
            break;
        case R.id.back:
            int dur = mMediaPlayer.getCurrentPosition();
            int pos = (dur > 10000 ? dur - 5000 : 0);
            mMediaPlayer.seekTo(pos);
            break;
        case R.id.fwd:
            int curpos = mMediaPlayer.getCurrentPosition();
            int dur2 = mMediaPlayer.getDuration();
            int pos2 = (curpos + 5000 > dur2 ? dur2 : curpos + 5000);
            mMediaPlayer.seekTo(pos2);
            break;
        }
    }
    private void notificationShow() {
        Context context = getApplicationContext();
        CharSequence contentTitle = "Mulugu Running";
        CharSequence contentText = url + " Weekly Predictions";
        Intent notifyIntent = new Intent(AudioView.this, AudioView.class);
        PendingIntent intent = PendingIntent.getActivity(AudioView.this, 0,
                notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
        notifyDetails.setLatestEventInfo(context, contentTitle, contentText,
                intent);
        mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
    }
    private void changePlayerIcons(boolean b) {
        if (b) {
            mPlay.setVisibility(android.view.View.INVISIBLE);
            mPause.setVisibility(android.view.View.VISIBLE);
        } else {
            mPlay.setVisibility(android.view.View.VISIBLE);
            mPause.setVisibility(android.view.View.INVISIBLE);
        }
    }
}

使用的 XML 是

    <?xml version="1.0" encoding="UTF-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/main_audio"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:gravity="center"
            android:orientation="vertical" >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.45"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/text"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="2dp"
                    android:ellipsize="marquee"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:gravity="center_horizontal"
                    android:marqueeRepeatLimit="marquee_forever"
                    android:scrollHorizontally="true"
                    android:singleLine="true"
                    android:text="     for more details check on  "
                    android:textColor="#FF0000"
                    android:textSize="20dp"
                    android:textStyle="italic" >
                </TextView>
            </LinearLayout>
            <RelativeLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="3dp" >
                <ImageView
                    android:id="@+id/pause"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="10dp"
                    android:src="@drawable/audio_pause" />
                <ImageView
                    android:id="@+id/play"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_marginLeft="2dp"
                    android:layout_marginTop="10dp"
                    android:src="@drawable/audio_play" />
                <ImageView
                    android:id="@+id/back"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/pause"
                    android:layout_marginRight="1dp"
                    android:layout_toLeftOf="@+id/pause"
                    android:layout_toRightOf="@drawable/audio_play"
                    android:src="@drawable/audio_bwd" />
                <ImageView
                    android:id="@+id/fwd"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/pause"
                    android:layout_marginLeft="1dp"
                    android:layout_toRightOf="@+id/pause"
                    android:src="@drawable/audio_fwd" />
            </RelativeLayout>
            <ProgressBar
                android:id="@+id/progressbar_Horizontal"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>
下面是

一个例子:

public class TextViewMarquee extends Activity {
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) this.findViewById(R.id.tv);  
        tv.setSelected(true);  // Set focus to the textview
    }
}

带有文本视图的 xml 文件:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:lines="1"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:textColor="#ff4500"
        android:text="Simple application that shows how to use marquee, with a long text" />
</RelativeLayout>

相关内容

  • 没有找到相关文章

最新更新