实现文本可点击:
方法一如下:
1、在layout的在xml文件中建一个TextView如下:
<TextView
android:id="@+id/forgetpass"
android:layout_width="fill_parent" //必要
android:layout_height="wrap_content" //必要
android:text="@string/hello"
android:clickable="true"//必要
/>
2、在src的java文件中,
在onCreate里面写以下代码
TextView forgetMessage = (TextView) findViewById(R.id.forgetpass);//获得TextView的id
forgetMessage.setOnClickListener(this);//监听点击
forgetMessage.setClickable(true);//实现可点击
3、让此activity继承implements OnClickListener
4、添加onClick方法;并在onClick方法里面添加如下代码
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.parse("http://www.baidu.com");//设置连接的网页
intent.setData(uri);
startActivity(intent);
}
方法二:java代码:
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class EventListen1 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClickButton(View v) {
// TODO Auto-generated method stub
if (v.getId() == (R.id.textview)){
Log.v( " click.. " , " textview " );
}
}
}
xml代码:
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation ="vertical"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
>
< TextView
android:id ="@+id/textview"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:onClick ="onClickButton"
android:clickable ="true"
android:text ="@string/hello"
/>
</ LinearLayout >
运行效果图: