gpt4 book ai didi

java - 如何在textView Fragment类中单击时获取消息中的数字

转载 作者:行者123 更新时间:2023-12-02 02:46:42 27 4
gpt4 key购买 nike

当从Fragment中单击textView时如何获取消息中的数据我在 android studio 工作我希望能得到代码,谢谢单击 Fragment 中的 textView 时如何获取消息中的数据我在 android studio 工作我希望能得到代码,谢谢

 <uses-permission android:name="android.permission.READ_CALL_LOG" />

非常感谢

enter image description here

import android.annotation.SuppressLint;
import android.content.CursorLoader;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;

/**
* Created by TAREQALEID1 on 09/06/2017.
*/

public class Tab1 extends Fragment {
View rootView;
boolean mDualPane;
int mCurCheckPosition = 0;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

Button btnCheckFalAction ;

rootView=inflater.inflate(R.layout.tab1,container,false);

ArrayList<HashMap<String,String>> callLog=getCallLog();
CustomCallLogListAdapter adapter=new CustomCallLogListAdapter(getActivity(),R.layout.row_call_log_layout,callLog);
ListView list_calllog=(ListView)rootView.findViewById(R.id.list_calllog);
list_calllog.setAdapter(adapter);




btnCheckFalAction = (Button) rootView.findViewById(R.id.button2); // you have to use rootview object..
btnCheckFalAction.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v)
{
Toast.makeText(getActivity(), "Hello World", Toast.LENGTH_LONG).show();
}
});

return rootView;
}
@Override
public void onActivityCreated(final Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
ListView list_calllog=(ListView)rootView.findViewById(R.id.list_calllog);




list_calllog.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Toast.makeText(getActivity(), "Item clicked : " + position, Toast.LENGTH_LONG).show();

}

});

}

@SuppressLint("NewApi")
public ArrayList<HashMap<String,String>> getCallLog()
{
ArrayList<HashMap<String,String>> callLog=new ArrayList<HashMap<String,String>>();
CursorLoader cursorLoader=new CursorLoader(getActivity(), CallLog.Calls.CONTENT_URI, null, null, null, null);
Cursor cursor=cursorLoader.loadInBackground();
if(cursor.moveToFirst())
{
while (cursor.moveToNext())
{
HashMap<String,String> hashMap=new HashMap<String, String>();
hashMap.put(CallLog.Calls.NUMBER, cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)));
hashMap.put(CallLog.Calls.DATE, cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE)));
hashMap.put(CallLog.Calls.DURATION, cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION)));
callLog.add(hashMap);
}
}
return callLog;
}


}

import android.content.Context;
import android.provider.CallLog;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;



public class CustomCallLogListAdapter extends ArrayAdapter<HashMap<String,String>>
{
private ArrayList<HashMap<String,String>> callLogData;
private Context context;
private int resource;
private View view;
private Holder holder;
private HashMap<String,String> hashMap;
public CustomCallLogListAdapter(Context context, int resource,ArrayList<HashMap<String, String>> objects)
{
super(context, resource, objects);
this.context=context;
this.resource=resource;
this.callLogData=objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{

LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(resource, parent,false);

holder=new Holder();
holder.text_number=(TextView)view.findViewById(R.id.text_calllog_number);
holder.text_date=(TextView)view.findViewById(R.id.text_calllog_date);
holder.text_time=(TextView)view.findViewById(R.id.text_calllog_time);

hashMap=callLogData.get(position);

Date date=new Date(Long.parseLong(hashMap.get(CallLog.Calls.DATE)));
java.text.DateFormat dateFormat=DateFormat.getDateFormat(context);
java.text.DateFormat timeformat=DateFormat.getTimeFormat(context);


holder.text_number.setText(hashMap.get(CallLog.Calls.NUMBER));
holder.text_time.setText(timeformat.format(date));
holder.text_date.setText(dateFormat.format(date));

return view;
}

public class Holder
{
TextView text_number;
TextView text_date;
TextView text_time;
}

}

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class Getfolder1 extends AppCompatActivity {

/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;

/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;

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

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));


}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";

public PlaceholderFragment() {
}

/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}


}

/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
switch (position){
case 0:
Tab1 tab1= new Tab1();
return tab1;

case 1:
Tab2 tab2= new Tab2();
return tab2;


}
return null;
}

@Override
public int getCount() {
// Show 3 total pages.
return 2;
}
}
}

<!-- begin snippet: js hide: false console: true babel: false -->

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/lyt1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="TextView"
tools:ignore="HardcodedText"
tools:text="مرحبا" />

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Button"
tools:ignore="HardcodedText" />

<ListView
android:id="@+id/list_calllog"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>
</android.support.constraint.ConstraintLayout>

<!-- begin snippet: js hide: false console: true babel: false -->

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="imagingstudents2.com.imagingstudents2.Getfolder1">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_weight="1"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:title="@string/app_name">

</android.support.v7.widget.Toolbar>

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.design.widget.TabItem
android:id="@+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_1" />

<android.support.design.widget.TabItem
android:id="@+id/tabItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab_text_2" />



</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

row_call_log_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/const1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginBottom="@dimen/activity_horizontal_margin"
android:layout_marginLeft="@dimen/activity_vertical_margin"
android:layout_marginRight="@dimen/activity_vertical_margin"
android:layout_marginTop="@dimen/activity_horizontal_margin">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/text_calllog_number"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/holo_orange_light"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
android:id="@+id/lyott2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_calllog_number">

<TextView
android:id="@+id/text_calllog_date"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
tools:ignore="NestedWeights" />

<TextView
android:id="@+id/text_calllog_time"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>

</LinearLayout>

</android.support.constraint.ConstraintLayout>

最佳答案

向holder.text_number添加一个OnclickListener:

String message = "";  
holder.text_number.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
message = textview.getText().toString();
Toast.makeText(getActivity(), "Message : " + message, Toast.LENGTH_LONG).show();

}
});

现在用message字符串做任何你想做的事情。

关于java - 如何在textView Fragment类中单击时获取消息中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44469151/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com