当前位置: 移动技术网 > IT编程>开发语言>Java > 小白开发安卓程序之路(2-2)TextView(解决 点击button不跳转、跑马灯跑不起来、Android studio 启动时黑屏)

小白开发安卓程序之路(2-2)TextView(解决 点击button不跳转、跑马灯跑不起来、Android studio 启动时黑屏)

2020年07月17日  | 移动技术网IT编程  | 我要评论

一些快捷键

Ctrl + d 复制本行至下一行
Ctrl+Alt+空格 重新弹出可选项(比如对齐的几种方式)

TextView

错误1:点击TextVIew没跳转到另一个页面

可能是MainActivity.java里少了一句 startActivity(intent);

错误2:跑马灯跑不起来

原因在于跑马灯一直没被选中。 需要在TextViewActivity.java再声明一个mtv7,并在oncreat函数中加上:

mtv7=findViewById(R.id.tv_7);
mtv7.setSelected(true);

找到并设置选中就ok啦。

错误3:Android studio模拟器本来好好的,突然一直黑屏打不开了

原因在于模拟器默认使用Quick Boot方式,这种情况下每次关闭模拟器再打开时,会恢复上次的状态,如果上次关闭时出了什么问题,就会导致重新打开时一直异常。
解决办法:

  1. 打开模拟器管理
  2. 右击模拟器
  3. 选择“Cold Boot Now”即可
    如图:
    在这里插入图片描述

实战:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"
    android:orientation="vertical">
    <Button
        android:id="@+id/btn_textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"/>

</LinearLayout>

MainActivity.java:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button mbtnTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mbtnTextView=findViewById(R.id.btn_textview);
        mbtnTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转至TextView演示界面
                Intent intent=new Intent(MainActivity.this,TextViewActivity.class);
                startActivity(intent);
            }
        });
    }
}

activity_text_view.xml:

注意:
tv_1 字体的大小颜色
tv_2 显示不下使用…
tv_3 文字+icon(注意需要将图片放到指定文件夹内)
tv_4 tv_5 tv_6中划线、下划线
tv-7 跑马灯

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tv_test1"
        android:textColor="#0066ff"
        android:textSize="28sp"/>
        <!--1.这里的"@string/tv_test1"是在res-values-strings内写好的属性:
        <string name="tv_test1">小方哥哥</string>
        也可以直接写成 android:text="小方哥哥"
        2.字体的单位是sp,考虑碎片化-->
    <TextView
        android:id="@+id/tv_2"
        android:layout_width="100dp"
        android:maxLines="1"
        android:ellipsize="end"
        android:layout_height="wrap_content"
        android:text="@string/tv_test1"
        android:textColor="#39AA55"
        android:textSize="30sp"/>
        <!--maxlines 限制文字只能占一行,elliosize设置显示不下则用省略号,可选择省略哪一部分  -->
    <TextView
        android:id="@+id/tv_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="筛选"
        android:layout_marginTop="10dp"
        android:drawableRight="@drawable/icon_arrow_off"
        android:textColor="#000000"
        android:drawablePadding="10dp"
        android:textSize="28sp"/>
    <!--maxlines 限制文字只能占一行,elliosize设置显示不下则用省略号,可选择省略哪一部分
    drawableRight指的是在右边放入一张图片,需要提前把图片放到相应文件夹.-app-src-main-res-drawable-->
    <TextView
        android:id="@+id/tv_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="乌拉拉"
        android:textColor="#19EEFF"
        android:textSize="28sp"
        android:layout_marginTop="10dp"/>
    <!--layout_marginTop指的是上面的间隔  -->
    <TextView
        android:id="@+id/tv_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="玛卡巴卡"
        android:textColor="#50AADD"
        android:textSize="28sp"
        android:layout_marginTop="10dp"/>
    <TextView
        android:id="@+id/tv_6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#50AADD"
        android:textSize="28sp"
        android:layout_marginTop="10dp"/>
    <TextView
        android:id="@+id/tv_7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="小方哥哥小方哥哥小方哥哥小方哥哥小方哥哥小方哥哥小方哥哥小方哥哥小方哥哥"
        android:textColor="#FFCC00"
        android:textSize="30sp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        />
        <!--获得焦点时实现跑马灯    -->
</LinearLayout>

TextViewActivity.java:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Paint;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;

public class TextViewActivity extends AppCompatActivity {
    private TextView mTv4,mTv5,mtv6,mtv7;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_view);
        mTv4=findViewById(R.id.tv_4);
        mTv4.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        //中划线
        mTv4.getPaint().setAntiAlias(true);
        //去除锯齿
        mTv5=findViewById(R.id.tv_5);
        mTv5.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        //下划线
        mtv6=findViewById(R.id.tv_6);
        mtv6.setText(Html.fromHtml("<u>玛卡巴卡</u>"));
        //利用html语言实现下划线
        mtv7=findViewById(R.id.tv_7);
        mtv7.setSelected(true);

    }
}

效果:

在这里插入图片描述
在这里插入图片描述

本文地址:https://blog.csdn.net/weixin_45941945/article/details/107360162

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网