如何通过单击textview或按钮等任何视图在android中动态更改标题?

时间:2014-10-28 11:43:55

标签: android listview

当我点击textview时,我需要更改标题中的文字,我尽最大努力去做一些事情,但我没有得到结果 我会发布代码,如果有人可以帮助我并告诉我哪里出错了,那将会有很大的帮助

这是我的results.xml,其中我的listview和带有textview的标题(在java代码中设置的标题)就在那里,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/White" >

<RelativeLayout
    android:id="@+id/rltHeader"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="#27acc3" >

    <ImageView
        android:id="@+id/imgBackReuslt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:onClick="onClick"
        android:src="@drawable/arrow" />

    <TextView
        android:id="@+id/txtHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="@color/White"
        android:textSize="20dp"
        android:textStyle="bold" />
 </RelativeLayout> 

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/rltHeader"
    android:layout_marginRight="5dp"
    android:divider="@color/LightGray"
    android:dividerHeight="4px" >
</ListView>

</RelativeLayout>

这是我的另一个xml,我已经显示了每行的列表视图将如何出现

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >



    <RelativeLayout
     android:layout_width="match_parent"
     android:layout_height="100dp">

    <RelativeLayout
        android:id="@+id/rltRow"
        android:layout_width="17dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:background="@drawable/r_list" >
    </RelativeLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="83dp"
        android:layout_height="80dp"
        android:layout_alignBottom="@+id/rltRow"
        android:layout_alignParentRight="true" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageView1"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="22dp"
        android:layout_toRightOf="@+id/rltRow"
        android:text="TextView"
        android:textColor="@color/Black"
        android:textSize="14dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_alignLeft="@+id/textView2"
        android:text="TextView"
        android:textColor="@color/Black" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/rltRow"
        android:layout_alignLeft="@+id/textView2"
        android:text="TextView"
        android:textColor="@color/Black" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignTop="@+id/imageView1"
        android:text="TextView"
        android:textColor="@color/Black"
        android:textSize="12dp" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:text="TextView"
        android:textColor="@color/Black"
        android:textSize="12dp" />
    </RelativeLayout>

    </RelativeLayout>

现在是Java部分 这是List_Activity类,其中我显示了所有的文本视图,有6个文本视图,当单击时,一个活动将出现Results.java,对于所有文本视图,当单击相同的活动时,Results.java将被打开。

package com.demo.Test;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class List_Activity extends Activity {

ImageView iv;
TextView t1, t2, t3, t4, t5, t6;
String header_result = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
  //TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_info);
    iv = (ImageView)findViewById(R.id.imgBackReuslt);
    t1 = (TextView) findViewById(R.id.text1);
    t2 = (TextView) findViewById(R.id.text2);
    t3 = (TextView) findViewById(R.id.text3);
    t4 = (TextView) findViewById(R.id.text4);
    t5 = (TextView) findViewById(R.id.text5);
    t6 = (TextView) findViewById(R.id.text6);

    t1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // String str = headervalue.getText().toString();

            Intent intent = new Intent("com.demo.Test.RESULTS");
            intent.putExtra("Results for Self", header_result);

            startActivity(intent);

        }
    });
    t2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent("com.demo.Test.RESULTS");
            intent.putExtra("Results for Mary(Wife)", header_result);
            startActivity(intent);

        }
    });
    t3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // name = t3.getText().toString();
            Intent intent = new Intent("com.demo.Test.RESULTS");
            intent.putExtra("Results for Alex(Child-1)", header_result);
            startActivity(intent);

        }
    });
    t4.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent("com.demo.Test.RESULTS");
            intent.putExtra("Results for Steven(Child-2)", header_result);
            startActivity(intent);

        }
    });
    t5.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent("com.demo.Test.RESULTS");
            intent.putExtra("Results for Robert(Father)", header_result);
            startActivity(intent);

        }
    });
    t6.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent("com.demo.Test.RESULTS");
            intent.putExtra("Results for Diana(Mother)", header_result);
            startActivity(intent);

        }
    });

}

public void onClick(View v) {
    super.onBackPressed();
    finish();
}

}

现在是主要的类Results.java,单击描述上一课程的textviews时打开的活动

package com.demo.Test;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Results extends Activity{
TextView header;
ImageView iv;
ListView lv;
String title;

int[] images = { R.drawable.photo_bg, R.drawable.photo_bg,
        R.drawable.photo_bg, R.drawable.photo_bg, R.drawable.photo_bg,
        R.drawable.b_list, R.drawable.g_list, R.drawable.r_list,
        R.drawable.v_list };

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.results);
    iv = (ImageView) findViewById(R.id.imgBackReuslt);
    header = (TextView) findViewById(R.id.txtHeader);

    lv = (ListView) findViewById(R.id.listView1);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        title = extras.getString("header_result");
    }
    ResultsAdapter adapter = new ResultsAdapter(getBaseContext(), doc,
            diag, dt, images, docname, diagname);
    lv.setAdapter(adapter);
}

所以当我单击List_Activity中的textviews时,我必须进行哪些更改以便更改标题文本 欢迎任何建议

2 个答案:

答案 0 :(得分:1)

您的意思是在每项活动中更改操作栏的标题吗?

你可以通过调用这个简短的代码

来做到这一点
getActionBar().setTitle("the title you want");

您可以查看此链接http://developer.android.com/guide/topics/ui/actionbar.html

希望它有所帮助,并告诉我你是否需要其他任何东西;)

答案 1 :(得分:0)

根据你想要的

    t2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent("com.demo.Test.RESULTS");
            // this header_result you want pass it textview (txtHeader)
            // prefer don't write long description in key of your extra 
            intent.putExtra("Results for Mary(Wife)", header_result);
            startActivity(intent);

        }
    });

放入包含

的jave文件
 header = (TextView) findViewById(R.id.txtHeader);
header.setText("YourPassingValue"); // in your case getIntent().getStringExtra("Results for Mary(Wife)");

优化代码

如果你想从活动转移到另一个

Intent intent = new Intent (YourFirstActivity.this , SecondActivity.class);
intent.putExtra("Result", PassingVariable); // prefer to make the key of your extra short to make east to call back , PassingVariable is what ever you want pass to the anther activity 
startActivity(intent);
在Anther活动中

获取额外数据

在您的on create方法

String PassedData = getIntent().getStringExtra("Result"); // Result is Your used key

使用传递的数据做任何事情

在你的情况下,你想在Textview上设置它

t2.setText(PassedData);