Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中通过单击任何视图(如textview或按钮)动态更改标题?_Android_Listview - Fatal编程技术网

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

如何在android中通过单击任何视图(如textview或按钮)动态更改标题?,android,listview,Android,Listview,当我点击文本视图时,我需要更改标题中的文本,我尽了最大努力做了一些事情,但没有得到结果 我会发布代码,如果有人能帮我解决问题,告诉我哪里出了问题,这将是非常有帮助的 这是my results.xml,其中有我的listview和带有textview的标题(要在java代码中设置的标题) <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/

当我点击文本视图时,我需要更改标题中的文本,我尽了最大努力做了一些事情,但没有得到结果 我会发布代码,如果有人能帮我解决问题,告诉我哪里出了问题,这将是非常有帮助的

这是my 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>
}

现在是主类Results.java,该活动在单击上一个类描述的文本视图时打开

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);
}
那么,当我点击列表中的文本视图时,我必须进行哪些更改,以使标题文本发生相应的更改
欢迎任何建议

您的意思是更改每个活动中动作栏的标题吗

您可以通过调用此短代码来完成此操作

getActionBar().setTitle("the title you want");
你可以查看这个链接


希望有帮助,如果你还需要什么,请告诉我;)

根据您的需要

    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);
在另一项活动中,获取额外数据

在您的oncreate方法中

String PassedData = getIntent().getStringExtra("Result"); // Result is Your used key
用传递的数据做任何事情

在您的情况下,您希望将其设置为Textview

t2.setText(PassedData);

标题、窗口标题或这个文本视图是什么意思?首先在xml布局中创建一个公共标题或txtview,然后在所有页面中使用它。通过传递意图,你可以根据你的意愿改变needs@minafawzy在results.xml中,我有一个文本视图,我将其显示为xyz的结果,但我想要的是,在我的列表中,活动t1代表xyz,t2代表abc,因此,当单击t1时,一个活动打开,显示结果页面,它显示了XYZ的结果,当我点击t2时,我的文本应该改变,应该是ABC的结果,这个标题文本我说的是abt,我希望它根据我的needs@iffu好的,bt我在我的结果中调用intent的方式。java对吗???@arjunnarahari请检查我的答案这是你想要的还是不想要的我只需要知道一件事,在intent中第一个“类。这个”,它应该是我们创建intent对象的同一个类,是不是ryt?好的,在你搬到那里之后,你是否有多余的字符串将其设置为文本视图?我使用了getActionBar(),但它产生了一些问题,因此我决定使用intent,但不使用thanx进行回复。如果我将来有问题,请与mebro分享你的知识。我需要一些关于不同主题的帮助,你能给我你的电子邮件id以便我们在那里讨论吗
String PassedData = getIntent().getStringExtra("Result"); // Result is Your used key
t2.setText(PassedData);