实现文本可点击:
方法一如下:
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代码:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
package com.listen;
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代码:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<?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"
>
<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>
运行效果图: