如何执行TextView onclick。我的java代码是:
Introduction.java:
public class Introduction extends Activity implements View.OnClickListener{
ImageView menubutton;
TextView intro;
TextView menutext1;
TextView menutext2;
TextView menutext3;
TextView menutext4;
TextView menutext5;
TextView menutext6;
TextView menutext7;
TextView menutext8;
RelativeLayout menupanel;
ScrollView description;
Boolean move = false;
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.introduction);
menubutton=(ImageView)findViewById(R.id.menuViewButton);
menutext1=(TextView)this.findViewById(R.id.menu_item_1);
menutext2=(TextView)this.findViewById(R.id.menu_item_2);
menutext3=(TextView)this.findViewById(R.id.menu_item_3);
menutext4=(TextView)this.findViewById(R.id.menu_item_4);
menutext5=(TextView)this.findViewById(R.id.menu_item_5);
menutext6=(TextView)this.findViewById(R.id.menu_item_6);
menutext7=(TextView)this.findViewById(R.id.menu_item_7);
menutext8=(TextView)this.findViewById(R.id.menu_item_8);
menutext1.setOnClickListener(this);
menupanel=(RelativeLayout)findViewById(R.id.menuPanel);
description=(ScrollView)findViewById(R.id.descriptionScrollView);
intro=(TextView)findViewById(R.id.intro);
tv1=(TextView)findViewById(R.id.textView1);
menutext1.setOnClickListener(this);
menutext2.setOnClickListener(this);
menutext3.setOnClickListener(this);
menutext4.setOnClickListener(this);
menutext5.setOnClickListener(this);
menutext6.setOnClickListener(this);
menutext7.setOnClickListener(this);
menutext8.setOnClickListener(this);
menutext1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
move = false;
Animation slideright= AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.movingright_to_left);
menupanel.startAnimation(slideright);
description.startAnimation(slide);
intro.setText("Introduction");
}
});
menutext2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
move = false;
Animation slideright= AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.movingright_to_left);
menupanel.startAnimation(slideright);
description.startAnimation(slide);
intro.setText("Preface");
Toast toast=Toast.makeText(getApplicationContext(),"Inside preface", 1);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL,0, 130);
toast.show();
tv1.setText("This is the third pocket size edition of the Constitution of India in the diglot form. In this edition, the text of the Constitution of India has been brought up-to-date by incorporating therein all the amendments up to and including the Constitution (Ninety-Sixth Amendment) Act, 2011. \n\n 2. The Constitution applies to the State of Jammu and Kashmir with certain exceptions and modifications as provided in article 370 and the Constitution (Application to Jammu and Kashmir) Order, 1954. This Order has been included in Appendix I for facility of reference. Appendix II contains a re-statement of the exceptions and modifications. The text of the constitutional amendments relating to the Constitution (Forty-fourth Amendment) Act, 1978 and Constitution (Eighty-eighth Amendment) Act, 2003, which have not come in force, have been provided in Appendix III and IV respectively. ");
}
});
menutext3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
move = false;
Animation slideright= AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.movingright_to_left);
menupanel.startAnimation(slideright);
description.startAnimation(slide);
intro.setText("Preamble");
}
});
menutext4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
move = false;
Animation slideright= AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.movingright_to_left);
menupanel.startAnimation(slideright);
description.startAnimation(slide);
intro.setText("Index");
}
});
menutext5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
move = false;
Animation slideright= AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.movingright_to_left);
menupanel.startAnimation(slideright);
description.startAnimation(slide);
intro.setText("Articles");
}
});
menutext6.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
move = false;
Animation slideright= AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.movingright_to_left);
menupanel.startAnimation(slideright);
description.startAnimation(slide);
intro.setText("Schedules");
}
});
menutext7.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
move = false;
Animation slideright= AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.movingright_to_left);
menupanel.startAnimation(slideright);
description.startAnimation(slide);
intro.setText("Appendix");
}
});
menutext8.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
move = false;
Animation slideright= AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.movingright_to_left);
menupanel.startAnimation(slideright);
description.startAnimation(slide);
intro.setText("Amendment");
}
});
menubutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!move)
{
move = true;
Animation slideleft= AnimationUtils.loadAnimation(getBaseContext(), R.anim.move);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.moving);
menupanel.startAnimation(slideleft);
// introduction.startAnimation(slide);
}
else{
move = false;
Animation slideright= AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.movingright_to_left);
menupanel.startAnimation(slideright);
//introduction.startAnimation(slide);
}
}
});
}
public void preface()
{
move = false;
Animation slideright= AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.movingright_to_left);
menupanel.startAnimation(slideright);
description.startAnimation(slide);
intro.setText("Preface");
Toast toast=Toast.makeText(getApplicationContext(),"Inside preface", 1);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL,0, 130);
toast.show();
tv1.setText("This is the third pocket size edition ");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
introduction.xml代码: -
<RelativeLayout 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"
tools:context=".Introduction" >
<!-- Sliding Panel -->
<LinearLayout
android:id="@+id/slidingPanel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
android:orientation="vertical"
android:background="@android:color/white" >
<RelativeLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/blue_bg" >
<View
android:id="@+id/header_vertical_divider_1"
android:layout_width="2dp"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/menuViewButton"
android:background="@drawable/engraved_bg" />
<ImageView
android:id="@+id/menuViewButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:clickable="true"
android:contentDescription="@string/description"
android:src="@drawable/icon_menu"
android:visibility="visible" />
</RelativeLayout>
<View
android:id="@+id/dividerHeaderBottom"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#414141" />
<TextView
android:id="@+id/intro"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#d3d3d3"
android:gravity="center"
android:text="CONSTITUTION OF INDIA"
android:textSize="16dp" />
<ScrollView
android:id="@+id/menu"
android:layout_width="137dp"
android:layout_height="match_parent"
android:layout_marginTop="0dp"
android:scrollbars="none" >
<RelativeLayout
android:id="@+id/menuPanel"
android:layout_width="150dp"
android:layout_height="fill_parent"
android:background="@drawable/gray_bg"
android:gravity="right"
android:orientation="vertical"
android:visibility="invisible" >
<TextView
android:id="@+id/menu_item_1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:textColor="@android:color/white"
android:text="@string/item_1"
android:clickable="true">
</TextView>
<View
android:id="@+id/menu_item_divider_1"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_below="@+id/menu_item_1"
android:background="#b5b5b5"/>
<TextView
android:id="@+id/menu_item_2"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:layout_below="@+id/menu_item_divider_1"
android:textColor="@android:color/white"
android:clickable="true"
android:text="@string/item_2">
</TextView>
<View
android:id="@+id/menu_item_divider_2"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_below="@+id/menu_item_2"
android:background="#b5b5b5"/>
<TextView
android:id="@+id/menu_item_3"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="@+id/menu_item_divider_2"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:textColor="@android:color/white"
android:text="@string/item_3"
android:clickable="true">
</TextView>
<View
android:id="@+id/menu_item_divider_3"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_below="@+id/menu_item_3"
android:background="#b5b5b5" />
<TextView
android:id="@+id/menu_item_4"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="@+id/menu_item_divider_3"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:textColor="@android:color/white"
android:text="@string/item_4"
android:clickable="true">
</TextView>
<View
android:id="@+id/menu_item_divider_4"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_below="@+id/menu_item_4"
android:background="#b5b5b5" />
<TextView
android:id="@+id/menu_item_5"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="@+id/menu_item_divider_4"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:textColor="@android:color/white"
android:text="@string/item_5"
android:clickable="true">
</TextView>
<View
android:id="@+id/menu_item_divider_5"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_below="@+id/menu_item_5"
android:background="#b5b5b5" />
<TextView
android:id="@+id/menu_item_6"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="@+id/menu_item_divider_5"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:textColor="@android:color/white"
android:text="@string/item_6"
android:clickable="true">
</TextView>
<View
android:id="@+id/menu_item_divider_6"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_below="@+id/menu_item_6"
android:background="#b5b5b5" />
<TextView
android:id="@+id/menu_item_7"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="@+id/menu_item_divider_6"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:textColor="@android:color/white"
android:text="@string/item_7"
android:clickable="true">
</TextView>
<View
android:id="@+id/menu_item_divider_7"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_below="@+id/menu_item_7"
android:background="#b5b5b5" />
<TextView
android:id="@+id/menu_item_8"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="@+id/menu_item_divider_7"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:textColor="@android:color/white"
android:text="@string/item_8"
android:clickable="true">
</TextView>
</RelativeLayout>
</ScrollView>
</LinearLayout>
<ScrollView
android:id="@+id/descriptionScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="92dp"
android:scrollbars="none">
<TextView
android:id="@+id/textView1"
android:layout_marginTop="1dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="India ie. Bharat is a Union of States. " />
</ScrollView>
在运行代码之后,没有执行menutext1,menutext2,......,menutext8 OnClickListener这些TextViews在滑块中的执行!????! 请帮助。 。 。
答案 0 :(得分:1)
尝试将TextView
设置为可点击。
textView.setClickable(true);
答案 1 :(得分:0)
public class Introduction extends Activity implements View.OnClickListener{
ImageView menubutton;
TextView intro;
TextView menutext1;
TextView menutext2;
TextView menutext3;
TextView menutext4;
TextView menutext5;
TextView menutext6;
TextView menutext7;
TextView menutext8;
RelativeLayout menupanel;
ScrollView description;
Boolean move = false;
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.introduction);
menubutton=(ImageView)findViewById(R.id.menuViewButton);
menutext1=(TextView)findViewById(R.id.menu_item_1);
menutext2=(TextView)findViewById(R.id.menu_item_2);
menutext3=(TextView)findViewById(R.id.menu_item_3);
menutext4=(TextView)findViewById(R.id.menu_item_4);
menutext5=(TextView)findViewById(R.id.menu_item_5);
menutext6=(TextView)findViewById(R.id.menu_item_6);
menutext7=(TextView)findViewById(R.id.menu_item_7);
menutext8=(TextView)findViewById(R.id.menu_item_8);
menutext1.setOnClickListener(this);
menupanel=(RelativeLayout)findViewById(R.id.menuPanel);
description=(ScrollView)findViewById(R.id.descriptionScrollView);
intro=(TextView)findViewById(R.id.intro);
tv1=(TextView)findViewById(R.id.textView1);
menutext1.setOnClickListener(this);
menutext2.setOnClickListener(this);
menutext3.setOnClickListener(this);
menutext4.setOnClickListener(this);
menutext5.setOnClickListener(this);
menutext6.setOnClickListener(this);
menutext7.setOnClickListener(this);
menutext8.setOnClickListener(this);
}
public void preface()
{
move = false;
Animation slideright= AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.movingright_to_left);
menupanel.startAnimation(slideright);
description.startAnimation(slide);
intro.setText("Preface");
Toast toast=Toast.makeText(getApplicationContext(),"Inside preface", 1);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL,0, 130);
toast.show();
tv1.setText("This is the third pocket size edition ");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getid())
{
case R.id.menu_item_1:
move = false;
Animation slideright= AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.movingright_to_left);
menupanel.startAnimation(slideright);
description.startAnimation(slide);
intro.setText("Introduction");
break;
case R.id.menu_item_2:
move = false;
Animation slideright= AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.movingright_to_left);
menupanel.startAnimation(slideright);
description.startAnimation(slide);
intro.setText("Preface");
Toast toast=Toast.makeText(getApplicationContext(),"Inside preface", 1);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL,0, 130);
toast.show();
tv1.setText("This is the third pocket size edition of the Constitution of India in the diglot form. In this edition, the text of the Constitution of India has been brought up-to-date by incorporating therein all the amendments up to and including the Constitution (Ninety-Sixth Amendment) Act, 2011. \n\n 2. The Constitution applies to the State of Jammu and Kashmir with certain exceptions and modifications as provided in article 370 and the Constitution (Application to Jammu and Kashmir) Order, 1954. This Order has been included in Appendix I for facility of reference. Appendix II contains a re-statement of the exceptions and modifications. The text of the constitutional amendments relating to the Constitution (Forty-fourth Amendment) Act, 1978 and Constitution (Eighty-eighth Amendment) Act, 2003, which have not come in force, have been provided in Appendix III and IV respectively. ");
break;
case R.id.menu_item_3:
move = false;
Animation slideright= AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.movingright_to_left);
menupanel.startAnimation(slideright);
description.startAnimation(slide);
intro.setText("Preamble");
break;
case R.id.menu_item_4:
move = false;
Animation slideright= AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.movingright_to_left);
menupanel.startAnimation(slideright);
description.startAnimation(slide);
intro.setText("Index");
break;
case R.id.menu_item_5:
move = false;
Animation slideright= AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.movingright_to_left);
menupanel.startAnimation(slideright);
description.startAnimation(slide);
intro.setText("Articles");
break;
case R.id.menu_item_6:
move = false;
Animation slideright= AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.movingright_to_left);
menupanel.startAnimation(slideright);
description.startAnimation(slide);
intro.setText("Schedules");
break;
case R.id.menu_item_7:
move = false;
Animation slideright= AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.movingright_to_left);
menupanel.startAnimation(slideright);
description.startAnimation(slide);
intro.setText("Appendix");
break;
case R.id.menu_item_8:
move = false;
Animation slideright= AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.movingright_to_left);
menupanel.startAnimation(slideright);
description.startAnimation(slide);
intro.setText("Amendment");
break;
case R.id.menubutton:
if(!move)
{
move = true;
Animation slideleft= AnimationUtils.loadAnimation(getBaseContext(), R.anim.move);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.moving);
menupanel.startAnimation(slideleft);
// introduction.startAnimation(slide);
}
else{
move = false;
Animation slideright= AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.movingright_to_left);
menupanel.startAnimation(slideright);
//introduction.startAnimation(slide);
}
}
}
}