【问题标题】:use textView as a button使用 textView 作为按钮
【发布时间】:2014-02-18 16:17:55
【问题描述】:

这是图片。我想单击目录中的体征和症状,然后单击我希望它直接跳转到体征和症状段落。请解决我的这个问题。这是我的代码。

<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"
     android:background="#ffffff"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Atherosclerosis"
    tools:ignore="UselessParent,ScrollViewCount" >
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<LinearLayout
     android:id="@+id/tablecontentlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#EFECCB" >
    <TextView
        android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17sp"
        android:textStyle="bold"
        android:textColor="#000000"
        android:text="@string/table" />
     <TextView
         android:id="@+id/definitionid"
         android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="#4682B4"
        android:text="@string/Definition" />
     <TextView
         android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="#4682B4"
        android:text="@string/RiskFactors" />
      <TextView
          android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="#4682B4"
        android:text="@string/Pathophysiology" />
      <TextView
          android:id="@+id/Signandsymptompsid"
          android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="#4682B4"
        android:text="@string/SignsandSymptoms" />
       <TextView
           android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="#4682B4"
        android:text="@string/Diagnosis" />
       <TextView
           android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="#4682B4"
        android:text="@string/Treatment" />
       <TextView
           android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="#4682B4"
        android:text="@string/Prevention" />
       <TextView
           android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="#4682B4"
        android:text="@string/Complications" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:layout_below="@+id/tablecontentlayout"
    android:layout_marginTop="15dp"
    android:orientation="vertical" >
        <TextView
            android:id="@+id/atherodefinition" 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Definition"
            android:background="#F5F5F5"
            android:textColor="#264177"
            android:textSize="17sp"
            android:textStyle="bold"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/atherosclerosisdefinition"
            android:textSize="15sp"/>
          <TextView

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/RiskFactors"
            android:background="#F5F5F5"
            android:textColor="#264177"
            android:textSize="17sp"
            android:textStyle="bold"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Modifiable"
            android:textStyle="bold"
            android:textSize="15sp"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/modifiableatherosclerosis"
            android:textSize="15sp"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Nonmodifiable"
            android:textStyle="bold"
            android:textSize="15sp"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/nonmodifiableatherosclerosis"
            android:textSize="15sp"/>
          <TextView

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Pathophysiology"
            android:background="#F5F5F5"
            android:textColor="#264177"
            android:textSize="17sp"
            android:textStyle="bold"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/pathophysiologyatherosclerosis"
            android:textSize="15sp"/>
        <TextView
            android:id="@+id/Signandsymptomsparagraph"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/SignsandSymptoms"
            android:background="#F5F5F5"
            android:textColor="#264177"
            android:textSize="17sp"
            android:textStyle="bold"/>
         <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/atherosclerosissignsandsymptoms"
            android:textSize="15sp"/>
         <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Diagnosis"
            android:background="#F5F5F5"
            android:textColor="#264177"
            android:textSize="17sp"
            android:textStyle="bold"/>
         <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Diagnosisatherosclerosis"
            android:textSize="15sp"/>
         <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Treatment"
            android:background="#F5F5F5"
            android:textColor="#264177"
            android:textSize="17sp"
            android:textStyle="bold"/>
         <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/lifestylemodification"
            android:textSize="15sp"/>
         <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/treatmentatherosclerosis"
            android:textSize="15sp"/>
         <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Prevention"
            android:background="#F5F5F5"
            android:textColor="#264177"
            android:textSize="17sp"
            android:textStyle="bold"/>
         <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Preventionatherosclerosis"
            android:textSize="15sp"/>
         <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Complications"
            android:background="#F5F5F5"
            android:textColor="#264177"
            android:textSize="17sp"
            android:textStyle="bold"/>
         <TextView 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/complicationatherosclerosis"
            android:textSize="15sp"/>
    </LinearLayout>
    </RelativeLayout>
    </ScrollView>

</RelativeLayout>

我的这个布局的java代码:

public class Atherosclerosis extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.atherosclerosis);

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.atherosclerosis, menu);
        return true;
    }

}

【问题讨论】:

  • 你也可以使用 ListView 来代替几个 TextView...

标签: java android


【解决方案1】:

使用android:onClick=""android:Clickable="true"

将 Textview 设为按钮

【讨论】:

  • 从目录直接跳转到段落如何编码?
  • view.smoothScrollToPosition(positionofContent);
  • 你能告诉我这个位置我应该写什么吗?请看我的布局。我已经给出了 id 标志和症状段落。
【解决方案2】:

您可以在TextView 上使用OnClickListener,然后在OnClick 上使用以下方法,其中textViewAbove 是您需要跳转的TextView 上方:

scrollView.post(
    new Runnable() {
        @Override
        public void run() {
            new CountDownTimer(1500, 20) {
                @Override
                public void onTick(long millisUntilFinished) {
                    scrollView.scrollTo(0, (int) (textViewAbove.getBottom()-millisUntilFinished));
                }

               @Override
               public void onFinish() {                 
               }
            }.start();
        }
    }
);

希望对你有帮助!

【讨论】:

  • 您的 TextViews 需要 id。因此,如果您单击风险因素,例如,您可以使用 findViewById 找到上面的文本视图,在这种情况下,描述文本视图具有 android:text="@string/atherosclerosisdefinition"。
  • 我应该在你的代码中写 textview id 而不是 textviewabove 吗??
  • textViewAbove 是变量,例如TextView textViewAbove = (TextView) findViewById(R.id.atherodefinition),就是要跳转的textview上面的textview(风险因素)。
  • 请看上面的java代码。还是行不通?我在代码中缺少什么?请指出来。
【解决方案3】:

您可以在TextView 上拥有OnclickListener

【讨论】:

  • 是的,我知道,但标题和段落的布局相同。我希望当我从图片中给出的目录中单击符号和症状时,它会自动向下滚动到相同布局中给出的段落的符号和症状??
  • 您需要聚焦特定视图。请点击链接stackoverflow.com/questions/6831671/…
【解决方案4】:

这是代码,但仍然给我错误,直到我错了?

public class Atherosclerosis extends Activity{

    ScrollView scrollView;

    TextView tvabove=(TextView)findViewById(R.id.pathophysiology_id);

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @SuppressLint("NewApi")

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.atherosclerosis);
             ActionBar bar = getActionBar();
                bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#6B8E23")));
                getActionBar().setDisplayHomeAsUpEnabled(true);
                getActionBar().setHomeButtonEnabled(true); 
        TextView tv=(TextView)findViewById(R.id.Signandsymptompsid);

        tv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                scrollView.post(
                        new Runnable() {
                            @Override
                            public void run() {
                                new CountDownTimer(1500, 20) {
                                    @Override
                                    public void onTick(long millisUntilFinished) {
                                        scrollView.scrollTo(0, (int) (tvabove.getBottom()-millisUntilFinished));
                                    }

                                   @Override
                                   public void onFinish() {                 
                                   }
                                }.start();
                            }
                        }
                    );
            }
        });
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.atherosclerosis, menu);
            return true;
        }
          @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                // Take appropriate action for each action item click
                switch (item.getItemId()) {
                case R.id.id_search:
                    Intent newActivity0 = new Intent(this,Search.class);     
                     startActivity(newActivity0);
                    return true;
                case R.id.id_favorit:
                   return true;

                default:
                    return super.onOptionsItemSelected(item);
                }
            }

    }

【讨论】:

  • 把你的TextView tvabove=(TextView)findViewById(R.id.pathophysiology_id)放在setContentView(R.layout.atherosclerosis)之后,你需要使用findviewbyid获取scrollview才能使用scrollView.post ...如果您不断收到一些错误,您可以发布错误日志吗?
猜你喜欢
  • 2018-04-02
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多