在TextView中,有时会有需要根据内容格式来匹配动作,怎么理解呢
比如说我们看到一个网址如:www.baidu.com,我们希望能够连接过去,这时我们可以实现去自动识别网址,然后能够连接过去
废话少说,看代码:
activity_main.xml代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClick"
android:text="根据内容自动匹配动作的文本" />
</LinearLayout>
MainActivity.java代码:
package com.example.exercise3;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void myClick(View view){
Intent intent=new Intent();
switch(view.getId()){
case R.id.btn1:
intent.setClass(this, HtmlFormatText.class);
break;
case R.id.btn2:
intent.setClass(this, AutoMatch.class);
break;
case R.id.btn3:
intent.setClass(this, MyDefineActionTextView.class);
break;
case R.id.btn4:
intent.setClass(this, FormatTextView.class);
break;
case R.id.btn5:
intent.setClass(this,InputImageEditViewActivity.class);
break;
}
this.startActivity(intent);
}
}
点击btn2,启动AutoMatch.java
AutoMatch.java代码:
package com.example.exercise3;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class AutoMatch extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.auto_match);
TextView tv=(TextView)findViewById(R.id.match_txt);
String msg="百度:www.baidu.com\n";
msg+="8690423";
tv.setText(msg);
}
}
auto_match.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/match_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
/>
</LinearLayout>
看了这么多,其实实现功能的代码就是:android:autoLink="all",
以后再开发过程中这样的使用方式并不多
如果各位朋友有什么疑问和建议,欢迎大家给我留言。。。
最近发现Android4.0系统中ProgressDialog设置为setCancelable(true)时,点击ProgressDialog以外的区域也可以让ProgressDialog dismiss掉。但有时我们不希望是这样的效果,于是将ProgressDialog设置setCancelable(false),再点击ProgressDialog以外的区域就不会出现前面所说的情况了,然而这样以来再按返回键时就不起作用了,只能等待程序执行到dissmiss处为止。其实,只要为ProgressDialog添加KeyListener来对返回键予以处理就可以解决这个问题了。
今天看了一些QSettings的简单用法,可以用来保存程序的设置,使得程序每次启动都可以显示上次关闭时的状态。我这里实现了一个简单的文本编辑窗口,可以设置文本的字体,字体的颜色和背景色。每次关闭程序都保存程序的几何大小,位置和文本以及文本所设置的格式,方便启动程序后继续编辑。
文本编辑窗口class TextEditor:public QTextEdit { Q_OBJECT public: TextEditor(QWidget *parent = NULL); ~TextEditor(); void SaveSettings(); protected: void ReadSettings(); void contextMenuEvent ( QContextMenuEvent * event ); private slots: void SettingBackColorSlot(); void SettingTextColorSlot(); void SettingTextFontSlot(); };
TextEditor::TextEditor( QWidget *parent /*= NULL*/ ):QTextEdit(parent) { ReadSettings(); } TextEditor::~TextEditor() { } void TextEditor::contextMenuEvent( QContextMenuEvent * event ) { QMenu *pMenu = createStandardContextMenu(); pMenu->addSeparator(); QTextCursor cursor = this->textCursor(); QString seletedText = cursor.selectedText(); if (!seletedText.isEmpty()) //选中文本才可以设置文本样式 { QMenu *pSubMenu = new QMenu(tr("设置"),pMenu); pMenu->addMenu(pSubMenu); QAction *pFontAct = new QAction(tr("字体"),pSubMenu); QAction *pTextColorAct = new QAction(tr("字体颜色"),pSubMenu); QAction *pBackColorAct = new QAction(tr("背景色"),pSubMenu); pSubMenu->addAction(pFontAct); pSubMenu->addAction(pTextColorAct); pSubMenu->addAction(pBackColorAct); connect(pFontAct,SIGNAL(triggered ()),this,SLOT(SettingTextFontSlot())); connect(pTextColorAct,SIGNAL(triggered ()),this,SLOT(SettingTextColorSlot())); connect(pBackColorAct,SIGNAL(triggered ()),this,SLOT(SettingBackColorSlot())); } pMenu->exec(event->globalPos()); delete pMenu; }
//设置文本背景色 void TextEditor::SettingBackColorSlot() { QColor color = QColorDialog::getColor(Qt::white, this, "Select Color", QColorDialog::DontUseNativeDialog); if(color.isValid()) { this->setTextBackgroundColor(color); } } //设置文本颜色 void TextEditor::SettingTextColorSlot() { QColor color = QColorDialog::getColor(Qt::black, this, "Select Color", QColorDialog::DontUseNativeDialog); if(color.isValid()) { this->setTextColor(color); } } //设置文本字体 void TextEditor::SettingTextFontSlot() { bool ok; QFont font = QFontDialog::getFont(&ok, this); if (ok) { QTextCursor cur = this->textCursor(); QString sltStr = cur.selectedText(); this->cut(); QTextCharFormat fmtText; fmtText.setFont(font); cur.insertText(sltStr,fmtText); } } //退出前保存文本信息 void TextEditor::SaveSettings() { QSettings TextSettings("Mysoft","TextData"); QString html = this->toHtml(); TextSettings.setValue("text",html); } //启动时读取信息 void TextEditor::ReadSettings() { QSettings TextSettings("Mysoft","TextData"); QString html = TextSettings.value("text").toString(); this->setHtml(html); }
class TextEdit : public QMainWindow { Q_OBJECT public: TextEdit(QWidget *parent = 0, Qt::WFlags flags = 0); ~TextEdit(); protected: void closeEvent ( QCloseEvent * event ) ; void ReadSettings(); private: TextEditor *m_pCentralWidget; };
TextEdit::TextEdit(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags) { m_pCentralWidget = new TextEditor(this); this->setCentralWidget(m_pCentralWidget); ReadSettings(); } TextEdit::~TextEdit() { } void TextEdit::closeEvent( QCloseEvent * event ) { QSettings dialogSettings("Mysoft","dialogData"); //保存窗口位置和大小 dialogSettings.setValue("Rect",this->rect()); QPoint pos = this->pos(); dialogSettings.setValue("positionX",this->pos().x()); dialogSettings.setValue("positionY",this->pos().y()); m_pCentralWidget->SaveSettings(); } void TextEdit::ReadSettings() { QSettings dialogSettings("Mysoft","dialogData"); //读取窗口位置和大小 dialogSettings.setValue("Rect",this->rect()); dialogSettings.setValue("position",this->pos()); QRect rect = dialogSettings.value("Rect").value<QRect>(); this->setGeometry(rect); int posX = dialogSettings.value("positionX").toInt(); int posY = dialogSettings.value("positionY").toInt(); this->move(QPoint(posX,posY)); }